Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
2  
Subsequent calls to dequeueReusableCellWithIdentifier will return different cells because that's what this method does: It's basically 'create a new cell for me, or give me a old one if it's no longer in use'. cellForRowAtIndexPath will always return the same cell as long as the cell is visilbe. If you scroll away and back again, the cell previously used may have been reused for another row. –  Vegar May 29 '13 at 10:51
    
Thank you Vegar. My problem was that I confused the table view's delegate tableView:cellForRowAtIndexPath: with the table view's method cellForRowAtIndexPath:. –  user1169629 May 29 '13 at 10:55

3 Answers 3

up vote 4 down vote accepted

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

on UITableView

share|improve this answer
    
lol thanks. First I didn't get it, now I see that I called the delegate directly and I feel dumb :) –  user1169629 May 29 '13 at 10:48

Use this to acces cell and modifying 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.

Hope it helps you.

share|improve this answer

It is for me also a little bit confusing. Because I do not know if its newly created or get existing one. For that case i use

- (NSArray *)visibleCells

method of UITableView. And get from that array. For determining which one is I looking for use either tag property or I add indexpath property to the cell, which are set in cellForRowAtIndexPath: method. If the cell is not in array, it is invisible anyway and will be created with cellForRowAtIndexPath: method, when user scrolls.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.