【问题标题】:Get a tableview insertRowsAtIndexPaths to accept indexOfObject?获取 tableview insertRowsAtIndexPaths 以接受 indexOfObject?
【发布时间】:2010-04-30 20:28:32
【问题描述】:

我的代码 sn-p 如下所示:

[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:编译时我也会收到警告。通常的“'arrayWithObject:' 的传递参数 1 使指针从整数而不进行强制转换......”(第 3 行)

以这个 sn-p 结束:

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

【问题讨论】:

    标签: iphone uitableview nsmutablearray nsindexpath


    【解决方案1】:

    您可以使用NSIndexPath 类扩展方法+indexPathForRow:inSection: 将行转换为索引路径。更多详情here.

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

    【讨论】:

      【解决方案2】:
      [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];
      

      【讨论】:

      • 我这样做的原因是因为新索引位于数组中的当前 indexPath 处。 (如果 indexPath 为 2,则数组中 2 的值可能为 20)。
      • 啊,好的。您发布的代码所做的是在索引 2 处获取对象,在索引 2 处查找对象的索引,并使用该对象创建一个数组。我在上面添加了更多代码,可能更接近您的需要。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-11
      • 2012-06-17
      • 1970-01-01
      • 2013-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多