获取重新sorting的UITableViewCells的顺序

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

viewDidLoad

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

//创build单元格时将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; } 

validation我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似乎是错误的。 我没有得到独特的价值。

 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 

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

代表和DataSource澄清

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

 -tableView:targetIndexPathForMoveFromRowAtIndexPath:toProposedIndexPath: 

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

这里的关键是,这个方法是UITableViewDelegate一部分, 它并不是要修改数据 ,而是改变tableView的外观,只是在单元格hover的时候做“滑动”animation。 返回的值决定了单元格的animation位置。

你应该做的是在用户提交时执行更改,当你调用UITableViewDataSource协议方法时会发生这种情况:

 -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, DA ])。 重要的是做出改变,以免互相影响。

 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) 

我终于find了一个适用于Apple Docs的解决scheme。

声明一个NSMutableArray属性.h或.m: @property (strong, nonatomic) NSMutableArray *arrayTag;

合成它: @synthesize arrayTag;

viewDidLoad初始化它arrayTag = [[NSMutableArray alloc] init];

这里是代码:

 -(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath { NSString *stringToMove = [arrayTag objectAtIndex:sourceIndexPath.row]; [arrayTag removeObjectAtIndex:sourceIndexPath.row]; [arrayTag insertObject:stringToMove atIndex:destinationIndexPath.row]; } 

链接到苹果网站:程序清单8-2: http : //developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/ManageReorderRow/ManageReorderRow.html

如果要交换两个对象,则必须在临时位置保存一个对象:

伪码

 arr = [a,b,c] //to swap(0,2)... tmp = arr[2] [arr replaceObjectAtIndex: 2 withObject: arr[0]] [arr replaceObjectAtIndex: 0 withObject:tmp];