'移动单元格'未在tableView中正确更新

时间:2013-07-18 20:40:24

标签: ios objective-c uitableview

我遇到了在编辑模式下在tableView中重新排列单元格的问题。向下移动单元格工作正常,但移动单元格会对顺序造成严重破坏。有时它会回到正常位置,有时候它会选择一个随机点来结束。具有讽刺意味的是,核心数据模型和订单每次都正确结束。

我搜索了STO并且确实没有找到解决方案。

- 没有应用手势控制,我也试过禁用它们。

-Tried [self setSuspendAutomaticTrackingOfChangesInManagedObjectContext:NO];

-Tried [self.tableView.canCancelContentTouches = NO];

-NSFetchControls / delegates绕过BOOL

我用来移动单元格的代码基本上与:

相同
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[_tableView setEditing:editing animated:animated];
if(editing) {
    NSInteger rowsInSection = [self tableView:_tableView numberOfRowsInSection:0];
   // Update the position of all items
   for (NSInteger i=0; i<rowsInSection; i++) {
      NSIndexPath *curIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
      SomeManagedObject *curObj = [_fetchedResultsController objectAtIndexPath:curIndexPath];
      NSNumber *newPosition = [NSNumber numberWithInteger:i];
      if (![curObj.displayOrder isEqualToNumber:newPosition]) {
         curObj.displayOrder = newPosition;
      }
   }
}
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
    NSInteger moveDirection = 1;
    NSIndexPath *lowerIndexPath = toIndexPath;
    NSIndexPath *higherIndexPath = fromIndexPath;
    if (fromIndexPath.row < toIndexPath.row) {
    // Move items one position upwards
    moveDirection = -1;
    lowerIndexPath = fromIndexPath;
    higherIndexPath = toIndexPath;
}

// Move all items between fromIndexPath and toIndexPath upwards or downwards by one position
for (NSInteger i=lowerIndexPath.row; i<=higherIndexPath.row; i++) {
    NSIndexPath *curIndexPath = [NSIndexPath indexPathForRow:i inSection:fromIndexPath.section];
    SomeManagedObject *curObj = [_fetchedResultsController objectAtIndexPath:curIndexPath];
    NSNumber *newPosition = [NSNumber numberWithInteger:i+moveDirection];
    curObj.displayOrder = newPosition;
}

SomeManagedObject *movedObj = [_fetchedResultsController objectAtIndexPath:fromIndexPath];
movedObj.displayOrder = [NSNumber numberWithInteger:toIndexPath.row];
NSError *error;

if (![_fetchedResultsController.managedObjectContext save:&error]) {
    NSLog(@"Could not save context: %@", error);
} else {
   [self.tableview reloadData];
}
}

希望有人能回答这个问题,因为回答STO问题的人真是太棒了! :)提前致谢!!!

-UPDATE-

我发现当我向上移动单元格时,该方法会不断引用同一个对象并将其更新为新顺序,而不是使用新顺序更新下一个对象。

即。向上移动单元格 - 结果=(Object1.order = 1,Object1.order = 2,Object1.order = 3)

即。向下移动单元格 - 结果=(Object1.order = 1,Object2.order = 2,Object3.order = 0)

2 个答案:

答案 0 :(得分:0)

更改

for (NSInteger i=lowerIndexPath.row; i<=higherIndexPath.row; i++)

for (NSInteger i=fromIndexPath.row; i<=toIndexPath.row; i -= moveDirection)

答案 1 :(得分:0)

在尝试了很多事情之后,我又采用了苹果文档的方式。对于遇到与我相同问题的其他人,创建一个你从coreData获得的NSArray的mutablecopy,并使用apple docs插入和删除索引的方法。它似乎解决了我的问题。 :)

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html