gpt4 book ai didi

ios - NSFetchedResultsController W/Custom Table Cell Incompatible Pointer

转载 作者:行者123 更新时间:2023-11-29 12:58:28 24 4
gpt4 key购买 nike

我一直在努力掌握 CoreData。经过多次尝试找到一个好的教程后,我在 tutorial on youtube 方面取得了很大进步。 .但是,当我到达 NSFetchedResultsController 时,我在自定义表格单元格 ITDContactCell *cell = [tableView cellForRowAtIndexPath:indexPath] 上收到警告,指出它是一个不兼容的指针初始化UITableViewCell(本教程不使用自定义单元格)。

更糟糕的是,当我尝试添加一个项目时,我得到了这个丑陋的错误。

2013-12-04 19:15:48.407 Ping[490:60b] * Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit/UIKit-2903.23/UITableView.m:1138 2013-12-04 19:15:48.409 Ping[490:60b] CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update with userInfo (null)

我花了几个小时试图弄清楚我做错了什么,但我无法全神贯注。因此,如果有人能看一看我的代码并就如何修复它提出建议,我将不胜感激。谢谢。

Controller :

-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.ContactsListTableView;
switch (type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate: {
Contact *changeContact = [self.fetchedResultsController objectAtIndexPath:indexPath];
ITDContactCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.name.text = [NSString stringWithFormat:@"%@ %@", changeContact.contactFirstName, changeContact.contactLastName];
}
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:newIndexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}

编辑:我对行数和节数的实现。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.fetchedResultsController sections]count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
return [sectionInfo numberOfObjects];
}

fetchedResultsController 在接口(interface)中简单定义为 @property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController

最佳答案

警告是指方法

[tableView cellForRowAtIndexPath:indexPath];

返回一个 UITableViewCell,它实际上不是您的 ITDContactCell。在该方法之前放置一个强制转换应该会删除警告。

ITDContactCell *cell = (ITDContactCell*) [tableView cellForRowAtIndexPath:indexPath];

另一个错误可能是因为您的 fetchedResultsController 和您的 tableView 之间缺少某种关系。显示您的 tableView 委托(delegate)和源方法会有所帮助。我假设您已经实现了类似的东西:

-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSError *error = nil;
if(![self.fetchedResultsController performFetch:&error]) {
// NSLog(@"Error! %@", error);
}
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return _fetchedResultsController.sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
return [secInfo numberOfObjects];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

ITDContactCell *cell = (ITDContactCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// assign cell content

return cell;
}

-(NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return [[[self.fetchedResultsController sections] objectAtIndex:section] name];
}

-(NSFetchedResultsController *) fetchedResultsController {
if(_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"EntityName"
inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}

-(void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
}

-(void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}

-(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.ContactsListTableView;
switch (type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate: {
Contact *changeContact = [self.fetchedResultsController objectAtIndexPath:indexPath];
ITDContactCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.name.text = [NSString stringWithFormat:@"%@ %@", changeContact.contactFirstName, changeContact.contactLastName];
}
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:newIndexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}

编辑 1:

请注意,您不应在 TableView 中使用 reloadData 方法,因为在保存对 NSManagedObjectContext 的更改后会重新加载表(通过获取的结果 Controller )。

编辑 2:

你的 NSManagedObjectContext 是 nil 吗?

关于ios - NSFetchedResultsController W/Custom Table Cell Incompatible Pointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20390754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com