Search code examples
iosios6uitableview

How do I call tableView:cellForRowAtIndexPath: safely in UITableView?


I have set up a tableview with a prototype cell in storyboard. Now I want to edit the cell's subview if it is swiped, so I implemented the delegate method tableView:willBeginEditingRowAtIndexPath:. How do I get the current cell being edited from inside this method? If I use tableView:cellForRowAtIndexPath: I get a new cell, not the one I need, because subsequent calls to dequeueReusableCellWithIdentifier:forIndexPath: seem to return different objects for the same identifier and indexPath.

I can reproduce this behaviour easily with the following code:

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

So when I can't use this method, how do I get the current cell that is placed at a specific indexPath? I'm using iOS 6.


Solution

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

    on UITableView