gpt4 book ai didi

iphone - 使用核心数据重新排列 UITableView

转载 作者:行者123 更新时间:2023-12-03 18:18:26 26 4
gpt4 key购买 nike

Possible Duplicate:
How to implement re-ordering of CoreData records?

我正在尝试找到一个代码示例,该示例展示了当单元格使用 fetchedResultsController (即与核心数据结合使用)时如何处理 tableView 中的移动/重新排列单元格。我收到 moveRowAtIndexPath: 对我的数据源的调用,但我找不到正确的黑魔法组合来让表/数据正确识别更改。

例如,当我将第 0 行移动到第 2 行然后放开时,它“看起来”是正确的。然后我点击“完成”。向上滑动填充第 0 行的行 (1) 仍具有其编辑模式外观(减号和移动图标),而下面的其他行则滑回正常外观。如果我向下滚动,当第 2 行(最初是 0,还记得吗?)接近顶部时,它会完全消失。

WTF。我是否需要以某种方式使 fetchedResultsController 无效?每当我将它设置为零时,我就会崩溃。我应该释放它吗?我是在杂草丛中吗?

这是我目前所拥有的内容...

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

NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];

/*
Update the links data in response to the move.
Update the display order indexes within the range of the move.
*/

if (fromIndexPath.section == toIndexPath.section) {

NSInteger start = fromIndexPath.row;
NSInteger end = toIndexPath.row;
NSInteger i = 0;
if (toIndexPath.row < start)
start = toIndexPath.row;
if (fromIndexPath.row > end)
end = fromIndexPath.row;
for (i = start; i <= end; i++) {
NSIndexPath *tempPath = [NSIndexPath indexPathForRow:i inSection:toIndexPath.section];
LinkObj *link = [fetchedResultsController objectAtIndexPath:tempPath];
//[managedObjectContext deleteObject:[fetchedResultsController objectAtIndexPath:tempPath]];
link.order = [NSNumber numberWithInteger:i];
[managedObjectContext refreshObject:link mergeChanges:YES];
//[managedObjectContext insertObject:link];
}

}
// Save the context.
NSError *error;
if (![context save:&error]) {
// Handle the error...
}

}

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {

// The fetch controller is about to start sending change notifications, so prepare the table view for updates.
if (self.theTableView != nil)
[self.theTableView beginUpdates];
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller has sent all current change notifications, so tell the table view to process all updates.
if (self.theTableView != nil) {
[self.theTableView endUpdates];
}
}

最佳答案

通常,当您看到类似的工件时,发生的情况是 UI 已动画到新位置并告诉您这一点,那么您对模型所做的更新无法正确反射(reflect)导致故障的状态下次 View 必须引用模型进行更新。

我认为您并不完全理解您应该在该方法中做什么。之所以调用它,是因为UI发生了变化,需要让模型也发生相应的变化。下面的代码假定结果已按新顺序排列,您只需要因某种原因重置顺序字段即可:

    for (i = start; i <= end; i++) {
NSIndexPath *tempPath = [NSIndexPath indexPathForRow:i inSection:toIndexPath.section];
LinkObj *link = [fetchedResultsController objectAtIndexPath:tempPath];
//[managedObjectContext deleteObject:[fetchedResultsController objectAtIndexPath:tempPath]];
link.order = [NSNumber numberWithInteger:i];
[managedObjectContext refreshObject:link mergeChanges:YES];
//[managedObjectContext insertObject:link];
}

问题是您实际上并没有更改底层模型中的顺序。这些索引路径来自 UITableViewController,它告诉您用户在这些路径之间拖动到点,您需要根据情况更新底层数据。但 fetchedResultsController 始终按排序顺序排列,因此在更改这些属性之前,任何内容都不会发生移动。

问题是,它们还没有被移动,你被叫来告诉你需要移动它们(通过调整可排序属性)。你真的需要更多类似的东西:

NSNumber *targetOrder = [fetchedResultsController objectAtIndexPath:toIndexPath];
LinkObj *link = [fetchedResultsController objectAtIndexPath:FromPath];
link.order = targetOrder;

这将导致对象重新排序,然后遍历并清理本应向上移动的其他对象的任何顺序号,并意识到索引可能已移动。

关于iphone - 使用核心数据重新排列 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1156380/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com