Iphone 使用NSFetchResultsController更改模型时,行不移动
我正在这里开发我的第一个iPhone应用程序(一个基本的待办事项列表应用程序),并且刚刚将NSFetchResultsController添加到我的表视图中。我有两部分行,添加行没有问题,它们是根据目标的completed属性是否为true进行排序的-如果为true,那么它将转到底部部分并将其划掉 每当您在顶部区域的行上向右滑动时,它会将目标的complete属性更改为true,并将其移动到底部区域:Iphone 使用NSFetchResultsController更改模型时,行不移动,iphone,ios,uitableview,core-data,nsfetchedresultscontroller,Iphone,Ios,Uitableview,Core Data,Nsfetchedresultscontroller,我正在这里开发我的第一个iPhone应用程序(一个基本的待办事项列表应用程序),并且刚刚将NSFetchResultsController添加到我的表视图中。我有两部分行,添加行没有问题,它们是根据目标的completed属性是否为true进行排序的-如果为true,那么它将转到底部部分并将其划掉 每当您在顶部区域的行上向右滑动时,它会将目标的complete属性更改为true,并将其移动到底部区域: RegimenGoal *goal = [_fetchedResultsControlle
RegimenGoal *goal = [_fetchedResultsController objectAtIndexPath:swipedIndexPath];
goal.completed = [NSNumber numberWithBool:YES];
[context save:&error];
这是我的
出于某种原因,这只是针对我刚刚创建的目标,而不是针对我添加的现有目标。经过一些调试后,我意识到NSFetchedResultsChangeMove并没有被触发,相反,只要我对非新的现有目标执行此操作,NSFetchedResultsChangeUpdate就会被触发
在苹果文档中也发现了这一点。我尝试了上面列出的工作,只是简单地检查完成的属性是否为真
当我这么做的时候,我发现newIndexPath并不是新的——它只是旧的indexPath,这让我很困惑。。。你知道为什么会这样吗 创建“获取结果”控制器时出错。如果通过指定
sectionNameKeyPath:@“completed”
将结果分组到节中,则必须使用相同的键添加第一个排序描述符:
NSSortDescriptor *completeSort = [[NSSortDescriptor alloc] initWithKey:@"completed" ascending:YES];
NSSortDescriptor *daySort = [[NSSortDescriptor alloc] initWithKey:@"dateCreated" ascending:YES];
[dayRequest setSortDescriptors:[NSArray arrayWithObjects:completeSort, daySort, nil]];
另一个问题在tableView:cellforrowatinexpath:
[self configureCell:cell atIndexPath:indexPath];
[cell formatCell:indexPath.section];
这里假设第0节包含completed=NO
的所有项目,第1节包含completed=YES
的所有项目。但如果所有项目都已完成,则只有一个部分(第0部分)包含所有已完成的项目。因此,不能使用indexPath.section
作为formatCell
的参数。您应该改用goal.completed
的值。例如,您可以将formatCell
调用移动到configureCell:atIndexPath:
方法:
- (void)configureCell:(RegimenCell *)cell atIndexPath:(NSIndexPath *)indexPath {
RegimenGoal *goal = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.label.text = goal.text;
[cell formatCell:goal.completed.intValue];
}
int goalsCount = [_tableView numberOfRowsInSection:0];
int completedCount = [_tableView numberOfRowsInSection:1];
现在,让表单元格reuseIdentifier
依赖于节和行号没有多大意义。我想你可以换一个
NSString *CellIdentifier = [NSString stringWithFormat:@"%d-%d", indexPath.row, indexPath.section];
用固定的字符串
NSString *CellIdentifier = @"YourCellIdentifer";
setNavTitle
方法中也存在类似问题:
- (void)configureCell:(RegimenCell *)cell atIndexPath:(NSIndexPath *)indexPath {
RegimenGoal *goal = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.label.text = goal.text;
[cell formatCell:goal.completed.intValue];
}
int goalsCount = [_tableView numberOfRowsInSection:0];
int completedCount = [_tableView numberOfRowsInSection:1];
同样,如果所有目标都完成了,它们都在第0节中,没有第1节。在这种情况下,当前代码将显示“(0%),而不是“(100%)
进一步评论:
- “移动的对象有时报告为已更新”的解决方法在这里似乎没有必要
- 对于
事件,您可以调用NSFetchedResultsChangeUpdate
或[自配置单元格:…]
。无需调用两者[[u tableView reloadRowsAtIndexPaths:…]
- 实例变量
应仅在\u fetchedResultsController
方法中使用,该方法可按需创建获取结果控制器(FRC)。在所有其他位置,应使用fetchedResultsController
确保在必要时创建FRCself.fetchedResultsController