2013-05-29 38 views
1

我已经在故事板中设置了一个原型单元格的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

+2

随后调用'dequeueReusableCellWithIdentifier'将返回不同的单元格,因为这就是这个方法的作用:它基本上是'为我创建一个新单元格,或者如果它不再被使用,给我一个旧单元格'。只要单元格可见,'cellForRowAtIndexPath'将始终返回相同的单元格。如果您再次滚动并返回,则以前使用的单元格可能已被重新用于另一行。 – Vegar

+0

谢谢你Vegar。我的问题是我混淆了表视图的委托tableView:cellForRowAtIndexPath:与表视图的方法cellForRowAtIndexPath :. – user1169629

回答

4

- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

UITableView

+0

大声笑谢谢。首先,我没有得到它,现在我看到我直接打电话给委托人,我感到无聊:) – user1169629

2

使用此细胞存取权限,并修改它:

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. 

希望它可以帮助你。

0

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

- (NSArray *)visibleCells 

UITableView的方法。并从数组中获得。为了确定哪一个是我想要使用的标记属性或我添加indexpath属性的单元格,这是在cellForRowAtIndexPath:方法中设置。如果单元格不在阵列中,则无论如何都不可见,并且将在用户滚动时使用cellForRowAtIndexPath:方法创建。

相关问题