ios - 如何在 UITableViewCell 中使用字典显示 NSArray 对象

标签 ios objective-c uitableview nsarray nsdictionary

这是我的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": @"Profile"},
                           @{@"text": @"Matches"},
                           @{@"text": @"Messages"},
                           @{@"text": @"Photos"},
                           @{@"text": @"Settings"},
                           @{@"text": @"Privacy"},
                           @{@"text": @"Reporting issues"},
                           ];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 {

    return sectionTitleArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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 单元格中。任何帮助都是可观的。

最佳答案

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

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/

相关文章:

objective-c - 如何在 iOS 中解析 RDF 文档

iphone - 绘图线的优化,CAShapeLayer 的可能替代品

ios - 多次调用 estimatedHeightForRowAtIndexPath 导致不稳定的 uitableview

objective-c - 滚动 UITableView 时单元格数据显示不正确

objective-c - Xcode/clang : Why do some, 不是全部,我的标题给 "warning: no rule to process file xxx for architecture arm7"

iphone - 如果我的应用程序图标上有角标(Badge)(来自推送),我可以转到特定 View 吗?

objective-c - validModesForFontPanel 从未调用过

ios - 在 iOS 中以编程方式切换 View

ios - Swift UIPickerView 第一个组件更改第二个组件数据

iOS SWIFT : Unable to delete user from Firebase Database