2013-04-14 53 views
0

我有一个表格视图,用户可以按照他希望的顺序移动和重新排列单元格。但是当他多次移动/重新排列单元格时,包含这些项目的数组会被完全搞乱,因为这些项目的顺序很关键,但是视觉上一切都很好。我究竟做错了什么?预先感谢您...在TableView中移动单元格

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath 
{ 

    id object1 = [_items5 objectAtIndex:sourceIndexPath.row]; 
    id object2 = [_items5 objectAtIndex:destinationIndexPath.row]; 

    [_items5 replaceObjectAtIndex:sourceIndexPath.row withObject:object2]; 
    [_items5 replaceObjectAtIndex:destinationIndexPath.row withObject:object1]; 

其中_items5是一个NSMutableArray和viewDidLoad中

初始化

回答

1

你交换两个项目,而不是移动的一个移动的项目。你想:

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

注意:如果使用MRC需要保留object所以之前将其放回数组中它不释放。

+0

哦..我现在明白了..谢谢! – user1780591