Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 使用NSFetchResultsController更改模型时,行不移动_Iphone_Ios_Uitableview_Core Data_Nsfetchedresultscontroller - Fatal编程技术网

Iphone 使用NSFetchResultsController更改模型时,行不移动

Iphone 使用NSFetchResultsController更改模型时,行不移动,iphone,ios,uitableview,core-data,nsfetchedresultscontroller,Iphone,Ios,Uitableview,Core Data,Nsfetchedresultscontroller,我正在这里开发我的第一个iPhone应用程序(一个基本的待办事项列表应用程序),并且刚刚将NSFetchResultsController添加到我的表视图中。我有两部分行,添加行没有问题,它们是根据目标的completed属性是否为true进行排序的-如果为true,那么它将转到底部部分并将其划掉 每当您在顶部区域的行上向右滑动时,它会将目标的complete属性更改为true,并将其移动到底部区域: RegimenGoal *goal = [_fetchedResultsControlle

我正在这里开发我的第一个iPhone应用程序(一个基本的待办事项列表应用程序),并且刚刚将NSFetchResultsController添加到我的表视图中。我有两部分行,添加行没有问题,它们是根据目标的completed属性是否为true进行排序的-如果为true,那么它将转到底部部分并将其划掉

每当您在顶部区域的行上向右滑动时,它会将目标的complete属性更改为true,并将其移动到底部区域:

  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
    应仅在
    fetchedResultsController
    方法中使用,该方法可按需创建获取结果控制器(FRC)。在所有其他位置,应使用
    self.fetchedResultsController
    确保在必要时创建FRC

非常感谢,Martin!我很感激。我不知道我也必须在排序描述符中包含complete,而且关于单元格格式和navtitle,你是对的。对于我的reuseIdentifier,我仍然需要以行/节的方式标记它,以便删除我正在子视图中的标签,因此我按目标进行标记。改为按完成。