开发者

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];


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?


[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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜