das*_*ist 0 objective-c nsdictionary uitableview ios
我目前有一个字典属性`scoreDictionary构造,以便它包含本身是嵌套字典的值:
{
Bob = {
"2013-08-02 00:27:02 +0000" = 70;
"2013-08-02 00:28:17 +0000" = 60;
};
Robert = {
"2013-08-02 02:53:19 +0000" = 1137;
};
Mooga = {
"2013-08-02 02:53:04 +0000" = 80;
};
}
Run Code Online (Sandbox Code Playgroud)
我能够为所需方法返回正确的行数tableView: numberOfRowsInSection:
,但在编写时遇到问题tableView: cellForRowAtIndexPath:
.具体来说,由于必须有两行显示相同的键"Bob",但是具有不同的得分和日期信息,我该如何处理indexPath.row
以便尽管具有相同的键,它每次调用时都能够为该单元返回不同的信息?
谢谢!
目前尚不清楚您希望输出看起来像什么.如果你想要部分,名称是部分标题,你可以像下面这样做.由于您的数据结构,获取正确的数据看起来很复杂(使用一组字典会更容易).
- (void)viewDidLoad {
[super viewDidLoad];
self.theData = @{@"Bob":@{@"2013-08-02 00:27:02 +0000":@70, @"2013-08-02 00:28:17 +0000":@60}, @"Robert":@{@"2013-08-02 02:53:19 +0000":@1137}, @"Mooga":@{@"2013-08-02 02:53:04 +0000":@80}};
self.keys = self.theData.allKeys;
[self.tableView reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
NSLog(@"sections are: %d",self.keys.count);
return self.keys.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.theData[self.keys[section]] count];
}
-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return self.keys[section];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
cell.textLabel.text = [self.theData[self.keys[indexPath.section]] allKeys][indexPath.row];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",[self.theData[self.keys[indexPath.section]] valueForKey:[self.theData[self.keys[indexPath.section]] allKeys][indexPath.row]] ];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
这给出了这个输出(使用正确的细节单元格类型):
如果您不想要节标题,可以删除titleForHeaderInSection方法,并将cellForRowAtIndexPath方法更改为:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSString *name = self.keys[indexPath.section];
NSString *date = [self.theData[self.keys[indexPath.section]] allKeys][indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",name,date];
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",[self.theData[self.keys[indexPath.section]] valueForKey:[self.theData[self.keys[indexPath.section]] allKeys][indexPath.row]] ];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
唯一的问题是,对于更长的名称,日期和时间字符串太长并且会切断数字值.因此,您可能需要缩短该字符串,或者转到字幕的字幕类型,其中该数字将位于单独的行上.
归档时间: |
|
查看次数: |
499 次 |
最近记录: |