NSFetchedResultsController W /自定义表格单元格不兼容的指针

时间:2013-12-05 03:19:25

标签: ios objective-c core-data nsfetchedresultscontroller

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

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

  

2013-12-04 19:15:48.407 Ping [490:60b] * 断言失败    - [UITableView _endCellAnimationsWithContext:],/ SourceCache / UIKit / UIKit-2903.23 / UITableView.m:1138 2013-12-04   19:15:48.409 Ping [490:60b] CoreData:错误:严重的应用程序错误。   从代表处获得了一个例外   NSFetchedResultsController在调用期间   -controllerDidChangeContent :.尝试将第0行插入第0部分,但在使用userInfo更新后第0部分中只有0行   (空)

我花了好几个小时试图弄清楚我做错了什么,但我无法绕过它。因此,如果任何人都可以对我的代码采取措辞,并给我提出如何解决它的建议,我将非常感激。谢谢。

控制器:

-(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在界面中定义为@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController

1 个答案:

答案 0 :(得分:1)

警告指的是方法

[tableView cellForRowAtIndexPath:indexPath];

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

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

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

-(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的更改后,将重新加载表(通过获取的结果控制器)。

编辑2:

你是NSManagedObjectContext nil?