Ios 如何在UITableView中安全地调用tableView:cellForRowAtIndexPath:?

Ios 如何在UITableView中安全地调用tableView:cellForRowAtIndexPath:?,ios,ios6,uitableview,Ios,Ios6,Uitableview,我在故事板中设置了一个带有原型单元的tableview。 现在我想编辑单元格的子视图(如果它被滑动),因此我实现了委托方法tableView:willBeginEditingRowAtIndexPath:。 如何从该方法中获取正在编辑的当前单元格?如果我使用tableView:CellForRowatineXpath:我会得到一个新的单元格,而不是我需要的单元格,因为对dequeueReusableCellWithIdentifier:forIndexPath:的后续调用似乎会为同一标识符和in

我在故事板中设置了一个带有原型单元的tableview。 现在我想编辑单元格的子视图(如果它被滑动),因此我实现了委托方法tableView:willBeginEditingRowAtIndexPath:。 如何从该方法中获取正在编辑的当前单元格?如果我使用tableView:CellForRowatineXpath:我会得到一个新的单元格,而不是我需要的单元格,因为对dequeueReusableCellWithIdentifier:forIndexPath:的后续调用似乎会为同一标识符和indexPath返回不同的对象

我可以使用以下代码轻松重现此行为:

- (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);
}
因此,当我无法使用此方法时,如何获取放置在特定索引XPath处的当前单元格? 我正在使用iOS 6。

-UITableViewCell*cellForRowAtIndexPath:NSIndexPath*indexPath

在UITableView上

-UITableViewCell*cellForRowAtIndexPath:NSIndexPath*indexPath


在UITableView上,使用此选项访问单元格并对其进行修改:

   NSIndexPath *indexPath = [NSIndexPath indexPathForRow:your_row inSection:your_section];
    UITableViewCell *currentCell = [you_table cellForRowAtIndexPath:indexPath];
    id anySubview = [currentCell viewWithTag:subview_tag]; // Here you can access any subview of currentcell and can modify it.

希望它能对您有所帮助。

使用此功能访问单元格并对其进行修改:

   NSIndexPath *indexPath = [NSIndexPath indexPathForRow:your_row inSection:your_section];
    UITableViewCell *currentCell = [you_table cellForRowAtIndexPath:indexPath];
    id anySubview = [currentCell viewWithTag:subview_tag]; // Here you can access any subview of currentcell and can modify it.

希望它能帮助你。

这对我来说也有点困惑。因为我不知道它是新创建的还是现有的。对于这种情况,我使用

- (NSArray *)visibleCells

UITableView的方法。并从该数组中获取。为了确定要查找哪一个,请使用标记属性或向单元格中添加indexpath属性,这些属性在cellForRowatineXpath:method中设置。如果单元格不在数组中,它无论如何都是不可见的,当用户滚动时,它将使用cellForRowAtIndexPath:method创建。

这对我来说也有点混乱。因为我不知道它是新创建的还是现有的。对于这种情况,我使用

- (NSArray *)visibleCells

UITableView的方法。并从该数组中获取。为了确定要查找哪一个,请使用标记属性或向单元格中添加indexpath属性,这些属性在cellForRowatineXpath:method中设置。如果单元格不在数组中,它无论如何都是不可见的,当用户滚动时,它将使用cellforrowatinexpath:method创建。

lol谢谢。一开始我没听懂,现在我看到我直接给代表打了电话,我觉得自己很傻:哈哈,谢谢。首先我没有理解它,现在我看到我直接调用了委托,我觉得自己很笨:对dequeueReusableCellWithIdentifier的后续调用将返回不同的单元格,因为这就是这个方法的作用:它基本上是“为我创建一个新的单元格,或者如果不再使用,给我一个旧的单元格”。cellForRowAtIndexPath将始终返回相同的单元格,只要该单元格是visilbe。如果您来回滚动,以前使用的单元格可能已重新用于另一行。谢谢Vegar。我的问题是,我混淆了表视图的委托tableView:CellForRowatineXpath:与表视图的方法CellForRowatineXpath:。对dequeueReusableCellWithIdentifier的后续调用将返回不同的单元格,因为该方法就是这样做的:它基本上是“为我创建一个新单元格,或者给我一个旧的,如果它不再使用。cellForRowAtIndexPath将始终返回相同的单元格,只要该单元格是visilbe。如果您来回滚动,以前使用的单元格可能已重新用于另一行。谢谢Vegar。我的问题是,我混淆了表视图的委托tableView:CellForRowatineXpath:与表视图的方法CellForRowatineXpath:。