简体   繁体   English

indexPath.row始终为0

[英]indexPath.row is always 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. 但是,该表填充了[数组计数]单元格数,其内容具有数组的索引0。 It seems to me that indexPath.row returns 0 for all cells. 在我看来,indexPath.row为所有单元格返回0。 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 编辑:这是我记录indexPath所得到的

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

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

Seems like the first index is updating while the second is not. 似乎第一个索引正在更新,而第二个索引没有。

When I logged indexPath.row, however 但是,当我记录indexPath.row时

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

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

So what should I use to get the value that is incrementing? 那么我应该使用什么来获得递增的值?

Thanks for the help again. 再次感谢您的帮助。

That block of code looks fine in itself. 这段代码本身看起来很好。 Maybe your tableview has multiple sections with one row each? 也许你的tableview有多个部分,每个部分有一行? What are you returning for the datasource methods tableView:numberOfRowsInSection: (should be [self.terms count] ) and numberOfSectionsInTableView: (should be 1). 你为数据源方法tableView:numberOfRowsInSection:返回了什么tableView:numberOfRowsInSection:应该是[self.terms count] )和numberOfSectionsInTableView:应该是1)。

If those are OK, put NSLog(@"%@",indexPath); 如果没有问题,请输入NSLog(@"%@",indexPath); somewhere in the method and post the output into your question. 方法中的某个位置并将输出发布到您的问题中。

I have just had a similar issue (indexPath.row was always 0) and noticed how much of an idiot I was. 我刚刚遇到了类似的问题(indexPath.row总是0),并注意到我有多少白痴。 In my numberOfSectionsInTableView I was returning the [dataSet count] and in my tableView:numberOfRowsInSection I was returning 1. Should be the other way around. 在我的numberOfSectionsInTableView我返回了[dataSet count]并在我的tableView:numberOfRowsInSection我正在返回1.应该是另一种方式。 Oops! 哎呀!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM