3

So here's my situation:

I have a UITableView where each cell has some buttons.
In the cellForRowAtIndexPath I set the indexpath of the row so when the a button is clicked I know what row was clicked.
One of the buttons is a delete button and when pressed I remove the backing data and delete the row from the table.

The problem I'm having is that after I delete a row the indexPath's of the other rows are incorrect as the other rows have not been redrawn. The only way I can find to redraw the table is to call reload but this messes up the animation.
I figure this kind of problem must have been addressed before. Does anyone have any suggestions on how to get around this? I'm ok with changing the way I've designed my buttons to work if there is a better way.

skorulis
  • 4,361
  • 6
  • 32
  • 43
  • Did you ever figure out a workaround for this? reloadRows and reloadSections have become a horrible problem for me because of the unwanted animations/flashes when there are supposed to be none. – SAHM Apr 24 '12 at 18:11

4 Answers4

9

Think you need to use this...

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

Apple reference!

Lee Armstrong
  • 11,420
  • 15
  • 74
  • 122
  • I've tried using the reloadSections version (as most of the section is incorrect) but the animations on this work out terrible. The whole table seems to flash and I can see the white that is behind the table. – skorulis Apr 07 '11 at 23:54
  • Maybe you need to reload the cells around it also? What is the data behind this as if you use CoreData it handles a lot of this for you. – Lee Armstrong Apr 08 '11 at 07:04
3

You should be able to redraw the tableview without a full reload by starting and ending an update like this:

[tableView beginUpdates];
[tableView endUpdates];
Moriya
  • 7,750
  • 3
  • 35
  • 53
2

call the method: - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

sycx
  • 891
  • 6
  • 8
  • I'm calling that method. That does the deletion correctly but doesn't redraw the other cells so the index paths are incorrect. – skorulis Apr 07 '11 at 07:38
1

It is better not to store the indexPath with the buttons. In the button action handler (which is either a method in your cell subclass or in your viewController) you could identify the cell, and subsequently determine the indexPath using -indexPathForCell:.

bio
  • 669
  • 4
  • 14
  • It's been a long time since I wrote this question but you are correct and this is how I now handle things. – skorulis Jan 20 '16 at 22:24