【问题标题】:'move cell up' not updating properly in tableView表视图中的“向上移动单元格”未正确更新
【发布时间】:2013-07-24 17:09:38
【问题描述】:

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

我已经通过 STO 进行了搜索,但确实没有找到解决方案。

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

-试过[self setSuspendAutomaticTrackingOfChangesInManagedObjectContext:NO];

-试过 [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 问题的人真是太棒了! :) 先谢谢了!!!

-更新-

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

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

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

【问题讨论】:

    标签: ios objective-c uitableview


    【解决方案1】:

    改变

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

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

    【讨论】:

    • 感谢您的回复,这对我没有影响,也许对其他人有影响。
    【解决方案2】:

    在尝试了很多事情之后,我重新使用了苹果文档的方式。对于与我有同样问题的其他人,请创建从 coreData 获得的 NSArray 的可变副本,并使用苹果文档从索引中插入和删除的方式。它似乎为我解决了这个问题。 :)

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      相关资源
      最近更新 更多