OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在 UITableViewCell 中使用字典显示 NSArray 对象 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:06
标题: ios - 如何在 UITableViewCell 中使用字典显示 NSArray 对象

这是我的 viewcontroller.m

@interface ViewController ()

{

    NSArray *sectionTitleArray;
}

   @property (strong, nonatomic) IBOutlet UITableView * tablevw;

- (void)viewDidLoad
{
    [super viewDidLoad];

    _tablevw.delegate = self;
    _tablevw.dataSource = self;



    sectionTitleArray = @[ @{@"text": @"About Step0ne"},
                           @{@"text": @"rofile"},
                           @{@"text": @"Matches"},
                           @{@"text": @"Messages"},
                           @{@"text": @"hotos"},
                           @{@"text": @"Settings"},
                           @{@"text": @"rivacy"},
                           @{@"text": @"Reporting issues"},
                           ];

}

- (NSInteger)numberOfSectionsInTableViewUITableView *)tableView
 {

    return sectionTitleArray.count;
}

-(NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section

{
    return 1;
}

-(UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath

{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.textLabel.text = [[sectionTitleArray objectAtIndex:indexPath.row]objectForKey"text"];

        return cell;
}

我是 Xcode 的新手,我在每个 tableview 单元格第一个对象中获得输出,即关于 Step0ne 正在显示,我需要所有对象都应显示在 UITableView 单元格中。任何帮助都是可观的。



Best Answer-推荐答案


由于您有“计数”部分,每个部分都有一行,因此您需要更改这一行:

cell.textLabel.text = [[sectionTitleArray objectAtIndex:indexPath.row]objectForKey"text"];

到:

cell.textLabel.text = [[sectionTitleArray objectAtIndex:indexPath.section]objectForKey"text"];

顺便说一句 - 这可以更容易地写成:

cell.textLabel.text = sectionTitleArray[indexPath.section][@"text"];

或者,如果您真的想要一个包含“计数”行的部分,请保留该行并相应地更新您的 numberOfRowsInSectionnumberOfSectionsInTableView

关于ios - 如何在 UITableViewCell 中使用字典显示 NSArray 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34648874/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4