去除fetchedresultscontroller代表和tableviewdatasouce第一部分之後經歷代碼和編輯一個小時後,的tableView正確顯示的項目:核心數據與TableView中得到斷言錯誤
CoreDataTVC:
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSections:(NSInteger)section
{
if(debug==1) NSLog(@"Running %@ '%@'",self.class,NSStringFromSelector(_cmd));
return [[self.frc.sections objectAtIndex:section] numberOfObjects];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{
if(debug==1) NSLog(@"Running %@ '%@'",self.class,NSStringFromSelector(_cmd));
return [[self.frc sections] count];
}
-(NSInteger)tableView:(UITableView*)tableView sectionForSectionIndexTitle:(NSString*)title atIndex:(NSInteger)index
{
if(debug==1) NSLog(@"Running %@ '%@'",self.class,NSStringFromSelector(_cmd));
return [self.frc sectionForSectionIndexTitle:title atIndex:index];
}
-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section
{
if(debug==1) NSLog(@"Running %@ '%@'",self.class,NSStringFromSelector(_cmd));
return [[[self.frc sections] objectAtIndex:section] name];
}
-(NSArray*)sectionIndexTitlesForTableView:(UITableView*)tableView
{
if(debug==1) NSLog(@"Running %@ '%@'",self.class,NSStringFromSelector(_cmd));
return [self.frc sectionIndexTitles];
}
-(void)controller:(NSFetchedResultsController*)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
if(debug==1) NSLog(@"Running %@ '%@'",self.class,NSStringFromSelector(_cmd));
switch(type)
{
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeMove:
break;
case NSFetchedResultsChangeUpdate:
break;
}
}
-(void)controller:(NSFetchedResultsController*)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath*)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath*)newIndexPath
{
if(debug==1) NSLog(@"Running %@ '%@'",self.class,NSStringFromSelector(_cmd));
switch(type)
{
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeMove:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeUpdate:
if(!newIndexPath)
{
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}else
{
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
break;
}
}
我我真的是ios編程的初學者,所以我仍然不明白我做錯了錯誤!上述代碼中的任何幫助對我來說都是一次很好的學習體驗!
如果我想使用委託方法,我應該改變什麼? – hengecyche
@hengecyche只實現'controllerDidChangeContent',並在那裏調用'[tableView reloadData]'。不要暗示其他委託方法。 – SolaWing
@hengecyche如果您的應用程序是單線程應用程序,您可以看到啓用CoreData的Xcode新項目模板。 – SolaWing