ios - 获取重新排序的 UITableViewCells 的顺序

标签 ios objective-c uitableview

我正在尝试将单元格的重新排序索引放入数组中。

viewDidLoad

cell.showsReorderControl = YES;
arrayTag = [NSMutableArray array];

//在创建单元格时将 indexPath.row 保存为 cell.tag。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
 {
   .....

   cell.tag = indexPath.row;
   NSString *strCellTag = [NSString stringWithFormat:@"%d",cell.tag];
   if(![arrayTag containsObject:strCellTag])
   {
      [arrayTag addObject:strCellTag];
   }
   return cell;
 }

//我不确定这是否是正确的方法,但下面我正在尝试将更改保存到数组中。
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath
{
    [arrayTag replaceObjectAtIndex:sourceIndexPath.row withObject:[NSString stringWithFormat:@"%i",proposedDestinationIndexPath.row]];
    [arrayTag replaceObjectAtIndex:proposedDestinationIndexPath.row withObject:[NSString stringWithFormat:@"%i",sourceIndexPath.row]];

    return proposedDestinationIndexPath;
}

验证我NSLog :
- (IBAction)doneButton:(id)sender
{
    NSLog (@"Number of Objects in Array %i", arrayTag.count);

    for (NSString *obj in arrayTag){
        NSLog(@"From ArrayTag obj: %@", obj);
    }   
}
NSLog没有按预期移动单元格的结果。
2013-07-10 17:50:47.291 MyApp[1634:c07] Number of Objects in Array 7
2013-07-10 17:50:47.292 MyApp[1634:c07] From ArrayTag obj: 0
2013-07-10 17:50:47.292 MyApp[1634:c07] From ArrayTag obj: 1
2013-07-10 17:50:47.292 MyApp[1634:c07] From ArrayTag obj: 2
2013-07-10 17:50:47.292 MyApp[1634:c07] From ArrayTag obj: 3
2013-07-10 17:50:47.293 MyApp[1634:c07] From ArrayTag obj: 4
2013-07-10 17:50:47.293 MyApp[1634:c07] From ArrayTag obj: 5
2013-07-10 17:50:47.293 MyApp[1634:c07] From ArrayTag obj: 6
NSLog移动/改组后的单元格似乎是错误的。我没有得到独特的值(value)。
2013-07-10 17:51:55.329 MyApp[1634:c07] Number of Objects in Array 7
2013-07-10 17:51:55.330 MyApp[1634:c07] From ArrayTag obj: 4
2013-07-10 17:51:55.330 MyApp[1634:c07] From ArrayTag obj: 5
2013-07-10 17:51:55.330 MyApp[1634:c07] From ArrayTag obj: 5
2013-07-10 17:51:55.330 MyApp[1634:c07] From ArrayTag obj: 5
2013-07-10 17:51:55.331 MyApp[1634:c07] From ArrayTag obj: 6
2013-07-10 17:51:55.331 MyApp[1634:c07] From ArrayTag obj: 6
2013-07-10 17:51:55.331 MyApp[1634:c07] From ArrayTag obj: 4

问:为什么重新排序后我没有获得新的唯一值?如何获取保存在数组中的新单元格顺序?

最佳答案

委托(delegate)和数据源说明

在以下情况下,您不应修改 tableView 后面的数据:

-tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath:

当用户将单元格悬停在其他单元格上方时会调用此方法,因此,在拖动单元格时会导致多次调用此函数。

这里的关键是,该方法是UITableViewDelegate的一部分,它不是要修改数据,而是改变tableView的外观,它只是在单元格时做“滑动”动画悬停。返回的值确定单元格的动画位置。

您应该做的是在用户提交更改时执行更改,这发生在您调用UITableViewDataSource协议(protocol)方法时:
-tableView:moveRowAtIndexPath:toIndexPath:

这是当用户释放单元格并且它就位时,每次拖放都会发生一次。

作为一般的经验法则,UITableViewDelegate上的内容是节目,UITableViewDataSource上的内容是真实数据。

修改模型

交换

交换或交换,是最简单的情况([ A , B, C, D ] => [ D , B, C, A ]),并且有一个非常方便的方法:
[arrayTag exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row];

迅速
let f = fromIndexPath.row, t = toIndexPath.row
(array[f], array[t]) = (array[t], array[f])

插入

插入有点复杂和冗长([ A , B, C, D ] => [B, C, D , A ])。重要的是进行更改,以免它们相互影响。
id obj = [arrayTag objectAtIndex:fromIndexPath.row];
[arrayTag removeObjectAtIndex:fromIndexPath.row];
[arrayTag insertObject:obj atIndex:toIndexPath.row];

迅速
let obj = array.removeAtIndex(fromIndexPath.row)
array.insert(newElement: obj, atIndex: toIndexPath.row)

关于ios - 获取重新排序的 UITableViewCells 的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17593068/

相关文章:

ios - 在 MFMessageComposeViewController 上设置 NavigationBar 背景颜色

ios - 滑动 ios 手势

ios - Quickblox - 获取用户 friend 的头像并显示在 UITableView 中

swift - tableviewcell 按钮中的弹出框位置不正确

ios - UITableView 中的两个不同的 UITableViewCell

ios - 无法转换 [Any] 类型的值!在强制 : in Swift3 while using for in loop 中键入 'MySpecificClass'

ios - Segue 加载下一个屏幕两次

ios - 尝试呈现模态视图 Controller 时出错

iphone - 如何使用 UIScrollview 动态加载图像

ios - 在屏幕上移动 UIImage