我已经在故事板中设置了一个原型单元格的tableview。 现在我想编辑单元格的子视图,如果它被滑过,所以我实现了委托方法tableView:willBeginEditingRowAtIndexPath :. 如何从此方法中获取正在编辑的当前单元格?如果我使用tableView:cellForRowAtIndexPath:我得到一个新的单元格,而不是我需要的单元格,因为随后调用dequeueReusableCellWithIdentifier:forIndexPath:似乎为相同的标识符和indexPath返回不同的对象。如何在UITableView中安全地调用tableView:cellForRowAtIndexPath:安全?
我可以用下面的代码很容易重现此问题:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSLog(@"cell = %@", cell);
cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSLog(@"different cell = %@", cell);
}
所以,当我不能使用这种方法,我怎么被放置在特定indexPath当前单元格? 我使用的是iOS 6
随后调用'dequeueReusableCellWithIdentifier'将返回不同的单元格,因为这就是这个方法的作用:它基本上是'为我创建一个新单元格,或者如果它不再被使用,给我一个旧单元格'。只要单元格可见,'cellForRowAtIndexPath'将始终返回相同的单元格。如果您再次滚动并返回,则以前使用的单元格可能已被重新用于另一行。 – Vegar
谢谢你Vegar。我的问题是我混淆了表视图的委托tableView:cellForRowAtIndexPath:与表视图的方法cellForRowAtIndexPath :. – user1169629