2011-11-20 139 views
12

我正在使用字典数组填充表视图。数组和字典的内容被解析没有问题。但是,该表中填充了[数组数量]的单元格,其内容索引为0。在我看来,indexPath.row为所有单元格返回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

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

再次感谢您的帮助。

+2

使用'indexPath.section' –

回答

36

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

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

+0

这里就是我有记录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] 我只有一个部分到我的表,我正在返回正确的值'tableView:numberOfRowsInSection:'和'numberOfSectionsInTableView:' –

+3

@SimonLee - 你看到的日志记录说表格视图是要求第0部分,第0行,然后第1部分,第0行。所以我不认为你的行和部分方法有正确的实现 - 你可以包括那些这个问题? – jrturton

+4

@jrturton感谢您的领导!原来,我在行中的段和段中实现了行。我切换后一切正常。愚蠢的错误! –

11

我刚刚有一个类似的问题(indexPath.row总是0),并注意到我有多少白痴。在我的numberOfSectionsInTableView我回来了[dataSet count]和在我的tableView:numberOfRowsInSection我回来了1.应该是相反的。哎呀!

+0

我也是...这是大声笑! – Dmitry

+0

不是唯一的白痴:)。谢谢 –