indexPath.row 始终为 0

发布于 2024-12-17 02:12:51 字数 1180 浏览 4 评论 0原文

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

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

再次感谢您的帮助。

I'm working on populating the table view with an array of dictionaries. The contents of the arrays and the dictionaries are parsed without problems. However, the table is populated with [array count] number of cells with contents with index 0 of the array. It seems to me that indexPath.row returns 0 for all cells. How do I populate each cell with the corresponding index?

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

Thanks a lot!

Edit: Here's what I got for logging indexPath

2011-11-21 03:07:58.025 Demo[21269:f803] 2
indexes [0, 0]

2011-11-21 03:07:58.027 Demo[21269:f803] 2
indexes [1, 0]

Seems like the first index is updating while the second is not.

When I logged indexPath.row, however

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

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

So what should I use to get the value that is incrementing?

Thanks for the help again.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画骨成沙 2024-12-24 02:12:51

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

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

That block of code looks fine in itself. Maybe your tableview has multiple sections with one row each? What are you returning for the datasource methods tableView:numberOfRowsInSection: (should be [self.terms count]
) and numberOfSectionsInTableView: (should be 1).

If those are OK, put NSLog(@"%@",indexPath); somewhere in the method and post the output into your question.

一笔一画续写前缘 2024-12-24 02:12:51

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

I have just had a similar issue (indexPath.row was always 0) and noticed how much of an idiot I was. In my numberOfSectionsInTableView I was returning the [dataSet count] and in my tableView:numberOfRowsInSection I was returning 1. Should be the other way around. Oops!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文