关于这方面有很多问题,但我无法找到问题的答案。我正在使用核心数据重新排序我的单元格,但是在编辑和滚动时我得到了重复的单元格。重新排序工作正常,如果我在编辑完成后重新加载视图,它们都是正确的顺序。如何重新加载移动的单元格以停止重复?
这是我的moveRow方法。就像我说它工作得很好,除了重复的细胞。
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
if ([fromIndexPath isEqual:toIndexPath]) {
return;
}
self.userDrivenDataModelChange = YES;
NSMutableArray *sortedObjects = [[self.fetchedResultsController fetchedObjects] mutableCopy];
// Get a handle to the call we're moving
MyObject *myObjectMovingFrom = [self.fetchedResultsController objectAtIndexPath:fromIndexPath];
MyObject *myObjectMovingTo = [self.fetchedResultsController objectAtIndexPath:toIndexPath];
int indexFrom = (int)[sortedObjects indexOfObject:myObjectMovingFrom];
int indexTo = (int)[sortedObjects indexOfObject:myObjectMovingTo];
// Remove the call from it's current position
[sortedObjects removeObjectAtIndex:indexFrom];
// Insert it at it's new position
[sortedObjects insertObject:myObjectMovingFrom atIndex:indexTo];
int i = 1;
for (MyObject *myObject in sortedObjects) {
NSLog(@"MyObject Moving: %@", myObject.myObjectName);
[myObject setStudioListPositionValue:i];
NSLog(@"MEW STUDIO LIST POSITION: %d", i);
i++;
}
NSError *error;
[self.managedObjectContext save:&error];
if (error) {
NSLog(@"%@", error);
}
self.userDrivenDataModelChange = NO;
}
我推荐的所有其他NSFetchedResultsController委托方法都包含在以下内容中。
if (self.userDrivenDataModelChange)
return;
{
...
}
如果我在我的移动方法中添加reloadRowsAtIndexPaths
,它会崩溃说我正在尝试重新加载不再存在的单元格。
如何刷新TableView以便我不会获得重复的单元格?
答案 0 :(得分:0)
使用以下内容删除对象后
[sortedObjects removeObjectAtIndex:indexFrom];
index在插入之前可能需要调整:
[sortedObjects insertObject:myObjectMovingFrom atIndex:indexTo];
我建议在这里添加一个断点来检查sortedObjects并确保结果是预期的。