Iphone 获取tableview insertRowsAtIndexPaths以接受indexOfObject?

Iphone 获取tableview insertRowsAtIndexPaths以接受indexOfObject?,iphone,uitableview,nsmutablearray,nsindexpath,Iphone,Uitableview,Nsmutablearray,Nsindexpath,我有一个代码段如下所示: [tableView beginUpdates]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft]; [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[array indexOfObject:[array objectA

我有一个代码段如下所示:

[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];
当用户点击附件时,它就会被执行。第一部分只是提供一个平滑的动画,实际上并不重要,因为tableView会在几毫秒后重新加载,但正如我所说的,它是用来提供动画的

它应该将选定对象从其当前indexPath移动到位于同一indexPath的数组中的值

显然,这段代码不起作用,所以我只想知道可以做些什么来修复它

PS:我在编译时也会收到警告。通常的“传递参数1的'arrayWithObject:'使指针不带强制转换的整数…”(第3行)

以以下片段结束:

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

您可以使用
nsindepath
类扩展方法
+indexPathForRow:instation:
将行转换为索引路径。更多细节

如果删除和插入行的唯一目的是引起动画,您是否考虑过
reloadRowsAtIndexPaths:withRowAnimation:
方法

[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[array indexOfObject:[array objectAtIndex:indexPath.row]]] withRowAnimation:UITableViewRowAnimationLeft];
把这条线分开,你会得到以下结果:

NSObject *obj = [array objectAtIndex:indexPath.row];
int *index = [array indexOfObject:obj];
NSArray *otherArray = [NSArray arrayWithObject:index];
[tableView insertRowsAtIndexPaths:otherArray withRowAnimation:UITableViewRowAnimationLeft];
您可能想要的是:

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];
但是为什么你不能这样做呢

NSArray *otherArray = [NSArray arrayWithObject:indexPath];
[tableView insertRowsAtIndexPaths:otherArray withRowAnimation:UITableViewRowAnimationLeft];
使用索引从数组中获取对象,然后使用对象查找索引似乎是多余的。只需使用索引


使用更多代码编辑:

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

之所以这样做,是因为新索引位于数组中当前的indexath。(如果indexPath是2,那么数组中2的值可能是20)。您发布的代码所做的是抓取索引2处的对象,查找索引2处对象的索引,并使用该对象创建一个数组。我在上面添加了一些代码,这些代码可能更接近您的需要。