objective-c - UITableView 源代码 : Array of dictionaries

标签 objective-c cocoa-touch uitableview

我正在构建一个 UITableView,它以字典数组为源。 每个字典都有一个字母作为键,以及一组联系人作为值,例如:键“A”-值“Adam、Alex、Andreas”。 我现在的问题是我无法获得每个部分的正确行数或部分标题...... 顺便说一句,我是 Objective-C 的新手,所以如果我的问题看起来很奇怪,我很抱歉。 非常感谢一些指导!

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    //tableContent is my array of dictionaries
    return [self.tableContent count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //here I don't know how to get the dictionary value array length that would be the
    //the number of contacts per letter
    return ?
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{
    //here I don't know how to get the dictionary key to set as section title
    retrun ?
}

最佳答案

您似乎无缘无故地将数据包装在数组中。如果您的数据只是一本字典,那对您来说会更容易

@property (nonatomic, strong) NSDictionary *tableContent;

...

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
  return [self.tableContent count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  NSString *key = [self tableView:nil titleForHeaderInSection:section];
  return [[self.tableContent objectForKey:key] count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{
  return [[self sortedKeys] objectAtIndex:section];
}

- (NSArray *)sortedKeys;
{
  return [self.tableContent.allKeys sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
}

关于objective-c - UITableView 源代码 : Array of dictionaries,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13255023/

相关文章:

iphone - 使用 GameKit 在 iPhone 之间传输 CoreData 数据

objective-c - 在 MAC 10.3.9 Cocoa 中解析和写入 XML?

iphone - 如何在iPhone应用程序之间共享自定义数据?

ios - 如何在 iOS 8 上检测预测文本

ios - 可变细胞永远不会突变

ios - 在 ios 中发送带有表格 View 和饼图的电子邮件

ios - 在 UITableViewCell 中加载 UIView

ios - [UIThreadSafeNode createPeripheral] : unrecognized selector sent to instance

iphone - CS193P - 添加取消按钮到 iOS 计算器

ios - otest 崩溃与 -[id<NSURLAuthenticationChallengeSender> userCredential :]