Core data UITableViewCell与UICollectionView
我正在学习这个教程 但我没有使用静态数组,而是将核心数据与nsfetchresultscontroller一起使用,我在委托方面遇到了问题Core data UITableViewCell与UICollectionView,core-data,nsfetchedresultscontroller,uicollectionviewcell,Core Data,Nsfetchedresultscontroller,Uicollectionviewcell,我正在学习这个教程 但我没有使用静态数组,而是将核心数据与nsfetchresultscontroller一起使用,我在委托方面遇到了问题 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } 我希望每个单元格只返回一个项目,其中包含所有项目: - (UITableViewCell *)tableView:(UITableView *)tabl
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
我希望每个单元格只返回一个项目,其中包含所有项目:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
ContainerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ContainerCell"];
NSArray *items = [[[self.frc sections] objectAtIndex:indexPath.section] objects];
[cell setCollectionData:items];
return cell;
}
但我得到了一个错误:
CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. attempt to delete row 1 from section 9 which only contains 1 rows before the update with userInfo (null)
如果我正在使用
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger rows = [super tableView:tableView numberOfRowsInSection:section];
return rows;
}
应用程序没有崩溃,但每个单元格都有重复的应用程序
有没有办法解决这个问题
感谢您在节中使用了非标准的行数,因此您必须确保在基础数据更改时删除并添加适当的行数。就你而言,没有 在控制器中查找错误删除或添加行的委托方法。您的错误消息中提到了它,应该
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
如您所见,在delete情况下,它执行以下操作:
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
但是indepath
没有描述托管对象在表视图中的位置,因为您正在单行中显示节的所有行。因此,您可以简单地删除这一行,并将其替换为该节中第一行的相应重载函数。也许您有一个configureCell
方法,如Xcode样板代码中所示:
NSIndexPath *row = [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
[tableView configureCell:[tableView cellForRowAtIndexPath:row] atIndexPath:row];
好的,我同意,但如果想删除或插入一些东西,如何将其传递给didChangeObject?这应该是显而易见的。