我正在构建一个 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/