12

我正在使用一系列字典填充表格视图。解析数组和字典的内容没有问题。但是,该表中填充了 [array count] 个单元格,其中包含数组索引为 0 的内容。在我看来, indexPath.row 为所有单元格返回 0 。如何使用相应的索引填充每个单元格?

- (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];
}


// Configure the cell...
NSDictionary *term = [self.terms objectAtIndex:indexPath.row];
cell.textLabel.text = [term objectForKey:@"content"];
cell.detailTextLabel.text = [term objectForKey:@"created"];

return cell;
}

非常感谢!

编辑:这是我记录 indexPath 的内容

2011-11-21 03:07:58.025 演示 [21269:f803] 2 个索引 [0, 0]

2011-11-21 03:07:58.027 演示 [21269:f803] 2 个索引 [1, 0]

似乎第一个索引正在更新,而第二个索引没有。

但是,当我记录 indexPath.row

2011-11-21 03:19:40.306 演示 [21546:f803] 0

2011-11-21 03:19:40.308 演示[21546:f803] 0

那么我应该使用什么来获得递增的值呢?

再次感谢您的帮助。

4

2 回答 2

38

该代码块本身看起来不错。也许您的 tableview 有多个部分,每个部分有一行?您为数据源方法tableView:numberOfRowsInSection:(应该是[self.terms count] )和numberOfSectionsInTableView:(应该是 1)返回什么。

如果这些都可以,NSLog(@"%@",indexPath);请在方法中的某个位置并将输出发布到您的问题中。

于 2011-11-20T06:55:13.550 回答
15

我刚刚遇到了类似的问题(indexPath.row 始终为 0)并注意到我是多么的白痴。在我的情况下,numberOfSectionsInTableView我正在返回,[dataSet count]而在我的情况下,tableView:numberOfRowsInSection我正在返回 1。应该反过来。哎呀!

于 2016-01-15T00:21:34.673 回答