【问题标题】:indexPath.row is always 0indexPath.row 始终为 0
【发布时间】:2011-11-20 06:40:11
【问题描述】:

我正在使用字典数组填充表格视图。解析数组和字典的内容没有问题。但是,该表中填充了 [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

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

再次感谢您的帮助。

【问题讨论】:

    标签: objective-c nsindexpath


    【解决方案1】:

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

    如果没有问题,请将NSLog(@"%@",indexPath); 放在方法中的某个位置,然后将输出发布到您的问题中。

    【讨论】:

    • 这是我记录 indexPath 的内容:2011-11-21 03:07:58.025 Demo[21269:f803] 2 个索引 [0, 0] 2011-11-21 03:07:58.027 Demo [21269:f803] 2 个索引 [1, 0] 我的表中只有一个部分,我正在为 tableView:numberOfRowsInSection:numberOfSectionsInTableView: 返回正确的值
    • @SimonLee - 您看到的日志显示表格视图要求第 0 节第 0 行,然后是第 1 节第 0 行。所以我认为您没有适合您的实现rows 和 section 方法 - 你可以在问题中包含这些方法吗?
    • @jrturton 感谢您的提醒!原来我在部分中实现了行,在行中实现了部分。切换它们后一切正常。愚蠢的错误!
    • @SimonLee 你应该接受大卫的回答,他是正确的。
    • 什么 F :D 谢谢哥们!
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-10
      • 2014-01-09
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-19
      相关资源
      最近更新 更多