Search code examples
iphoneuitableviewnsmutablearraynsindexpath

Get a tableview insertRowsAtIndexPaths to accept indexOfObject?


I have a code snippet that looks like this:

[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[array indexOfObject:[array objectAtIndex:indexPath.row]]] withRowAnimation:UITableViewRowAnimationLeft];
[tableView endUpdates];
[tableView reloadData];

It gets executed when a user clicks on an accessory. The first part is only there to provide a smooth animation, and does not really matter, as the tableView is reloaded milliseconds later, but as I said, it's there to provide an animation.

It is supposed to move a selected object from its current indexPath to the value from an array at the same indexPath.

Obviously, this code does not work, so I just want to know what can be done to fix it?

PS: I get a warning when compiling, too. The usual "passing argument 1 of 'arrayWithObject:' makes pointer from integer without a cast..." (line 3)

Ended up with this snippet:

[tableView beginUpdates];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:[array indexOfObject:[arraySubFarts objectAtIndex:indexPath.row]] inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];

[tableView reloadData];

Solution

  • You can use the NSIndexPath class extension method +indexPathForRow:inSection: to transform a row to an index path. More details here.

    If your only intention with deleting and inserting the row is to cause the animation, have you considered the reloadRowsAtIndexPaths:withRowAnimation: method?