Iphone 更改fetchedResults后tableView endUpdates崩溃
我有一个应用程序,当用户第一次启动应用程序时,它会显示一个自定义的无数据单元格 当用户输入第一个条目时,my fetchedResultsController的fetchedResults将更新,这将导致删除“尚未有数据”单元格并插入数据单元格 这在过去很管用(iPhone3.x)。现在在iOS 4.2上,调用endUpdates后会导致崩溃。没有异常信息或任何可理解的堆栈跟踪。我只知道崩溃是由cfuTypeCollectionRetain(可能是试图保留NULL)引起的 有什么办法吗 以下是相关代码:Iphone 更改fetchedResults后tableView endUpdates崩溃,iphone,uitableview,core-data,ios,Iphone,Uitableview,Core Data,Ios,我有一个应用程序,当用户第一次启动应用程序时,它会显示一个自定义的无数据单元格 当用户输入第一个条目时,my fetchedResultsController的fetchedResults将更新,这将导致删除“尚未有数据”单元格并插入数据单元格 这在过去很管用(iPhone3.x)。现在在iOS 4.2上,调用endUpdates后会导致崩溃。没有异常信息或任何可理解的堆栈跟踪。我只知道崩溃是由cfuTypeCollectionRetain(可能是试图保留NULL)引起的 有什么办法吗 以下是相
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
NSLog(@"starting updates");
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tv = self.tableView;
[tv reloadData];
switch(type) {
case NSFetchedResultsChangeInsert:
NSLog(@"insert");
if ([self.tableView numberOfRowsInSection:newIndexPath.section] == 1 &&
[[self.fetchedResultsController fetchedObjects] count] == 1)
{
NSLog(@"reloading row %d", newIndexPath.row);
[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[tv insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else {
NSLog(@"inserting new row %d", newIndexPath.row);
[tv insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
}
break;
case NSFetchedResultsChangeDelete:
NSLog(@"delete");
if ([self.tableView numberOfRowsInSection:newIndexPath.section] == 0 &&
[[self.fetchedResultsController fetchedObjects] count] == 0)
{
NSLog(@"reloading row %d", newIndexPath.row);
[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
[tv insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else {
NSLog(@"deleting row %d", newIndexPath.row);
[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
NSLog(@"finishing updates");
[self.tableView endUpdates];
}
-(void)controllerWillChangeContent:(NSFetchedResultsController*)控制器{
NSLog(@“启动更新”);
[self.tableView开始更新];
}
-(void)控制器:(NSFetchedResultsController*)控制器didChangeSection:(id)sectionInfo
atIndex:(nsInteger)ChangeType:(NSFetchedResultsChangeType)类型的节索引{
开关(类型){
案例NSFetchedResultsChangesInsert:
[self.tableView insertSections:[NSIndexSet IndexSetWithiIndex:sectionIndex]带RowAnimation:UITableViewRowAnimationFade];
打破
案例NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet IndexSetWithiIndex:sectionIndex]带RowAnimation:UITableViewRowAnimationFade];
打破
}
}
-(void)控制器:(NSFetchedResultsController*)控制器didChangeObject:(id)一个对象
atIndexPath:(NSIndexPath*)indexPath forChangeType:(NSFetchedResultsChangeType)类型
newindepath:(nsindepath*)newindepath{
UITableView*tv=self.tableView;
[电视重新加载数据];
开关(类型){
案例NSFetchedResultsChangesInsert:
NSLog(@“插入”);
如果([self.tableView numberofrowsinssection:newIndexPath.section]==1&&
[[self.fetchedResultsController fetchedObject]计数]==1)
{
NSLog(@“重新加载行%d”,newindexath.row);
[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]with RowAnimation:UITableViewRowAnimationFade];
[tv insertRowsAtIndexPaths:[NSArray ARRAY WITHOBJECT:newIndexPath]带行动画:UITableViewRowAnimationFade];
}
否则{
NSLog(@“插入新行%d”,newIndexPath.row);
[tv insertRowsAtIndexPaths:[NSArray ARRAY WITHOBJECT:newIndexPath]带行动画:UITableViewRowAnimationFade];
}
打破
案例NSFetchedResultsChangeDelete:
NSLog(@“删除”);
如果([self.tableView numberofrowsinssection:newIndexPath.section]==0&&
[[self.fetchedResultsController fetchedObject]计数]==0)
{
NSLog(@“重新加载行%d”,newindexath.row);
[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]with RowAnimation:UITableViewRowAnimationFade];
[tv insertRowsAtIndexPaths:[NSArray ARRAY WITHOBJECT:newIndexPath]带行动画:UITableViewRowAnimationFade];
}
否则{
NSLog(@“删除行%d”,newindexath.row);
[tv deleteRowsatindExpath:[NSArray arrayWithObject:indexPath]带RowAnimation:UITableViewRowAnimationFade];
}
打破
}
}
-(void)controllerDidChangeContent:(NSFetchedResultsController*)控制器{
NSLog(“完成更新”);
[self.tableView endUpdates];
}
在委托方法中
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
您可以处理插入和删除。对于插入indepath
是nil
。对于删除newIndexPath
是nil
。因此,如果type==NSFetchedResultsChangeDelete
,则不允许访问newindepath
此外,这里不需要调用reloadData
。在委托方法中
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
您可以处理插入和删除。对于插入indepath
是nil
。对于删除newIndexPath
是nil
。因此,如果type==NSFetchedResultsChangeDelete
,则不允许访问newindepath
此外,此处不需要调用reloadData