views:
672answers:
2Hey.
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];
+1
A:
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?
warrenm
2010-04-30 20:41:02
+1
A:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[array indexOfObject:[array objectAtIndex:indexPath.row]]] withRowAnimation:UITableViewRowAnimationLeft];
Taking this line and splitting it up you get the following:
NSObject *obj = [array objectAtIndex:indexPath.row];
int *index = [array indexOfObject:obj];
NSArray *otherArray = [NSArray arrayWithObject:index];
[tableView insertRowsAtIndexPaths:otherArray withRowAnimation:UITableViewRowAnimationLeft];
What you probably want is:
NSObject *obj = [array objectAtIndex:indexPath.row];
NSIndexPath *index = [NSIndexPath indexPathForRow:[array indexOfObject:obj] inSection:0];
NSArray *otherArray = [NSArray arrayWithObject:index];
[tableView insertRowsAtIndexPaths:otherArray withRowAnimation:UITableViewRowAnimationLeft];
But why can't you do this?
NSArray *otherArray = [NSArray arrayWithObject:indexPath];
[tableView insertRowsAtIndexPaths:otherArray withRowAnimation:UITableViewRowAnimationLeft];
Getting the object from the array using an index and then using the object to find the index seems redundant. Just use the index.
Edit with more code:
NSNumber *obj = [NSNumber numberWithInt:indexPath.row];
int *index = [array indexOfObject:obj];
NSIndexPath *index = [NSIndexPath indexPathForRow:index inSection:0];
NSArray *otherArray = [NSArray arrayWithObject:index];
[tableView insertRowsAtIndexPaths:otherArray withRowAnimation:UITableViewRowAnimationLeft];
MrHen
2010-04-30 20:50:20
the reason why I'm doing that is because the new index is at the current indexPath in an array. (if the indexPath is 2, value of 2 in the array might be 20).
Emil
2010-04-30 20:58:09
Ah, okay. What the code you posted is doing is grabbing the object at index 2, looking up the index of the object at index 2, and creating an array with that object. I added some more code above that may be closer what you need.
MrHen
2010-04-30 21:11:41
See updated first post, I fixed it :)
Emil
2010-05-01 07:49:49