本文介绍了更新由NSFetchedResultsController管理的UITableView,并在第一部分中有1个额外的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主要详细信息应用程序 -

Master Detail Application -

我有一个 UITableViewController ,由 NSFetchedResultsController 及其委托方法。我在tableview的第一部分还有一个额外的单元格,其中有一个 UIWebView ,它显示一个嵌入的视频。此单元格不是 NSFetchedResultsController 的一部分。当调用 -tableView cellForRowAtIndexPath 时,我将它添加到 IF 语句中,检查它是否是第一部分和第一行。一切工作伟大的大部分。我可以选择一行并显示在 DetailViewController 中,我可以从 tableView 中删除​​项目, DetailViewController tableViewCell 标签中的信息更新,没有任何问题。

I have a UITableViewController that is managed by an NSFetchedResultsController and its delegate methods. I also have one extra cell in the first section of the tableview that has a UIWebView in it that displays an embedded video. This cell is not part of the NSFetchedResultsController. I add it inside of an IF statement when -tableView cellForRowAtIndexPath is called, that checks to see if it's the first section and first row. Everything works great for the most part. I can select a row and display it in the DetailViewController, I can delete items from the tableView, I can change the information in the DetailViewControllerand the tableViewCell labels update with out any problem.

我的问题

我只有一个问题,它的设置是,当我删除最后一个对象从 tableView 的第一部分。

The only time I have an issue with the way it is setup is, when I delete the last object from the first section in the tableView.

错误

CODE

numberOfRowsInSection

这是我检查视频是否可用的部分,如果该部分为0,则返回1个额外的行。 else从 NSFetchedResultsController 返回对象计数。我假设这是我的问题所在。但是,我不知道如何解决它。因为我有它由 NSFetchedResultsController 委托方法管理在switch语句中的类型可以做任何事情?

This is where I check to see if the video is available and if the section is 0 return 1 extra row. else return the object count from the NSFetchedResultsController. I'm assuming this is where my issue is. However, I don't know how to fix it. Because I have it managed by the NSFetchedResultsController delegate methods is there any thing I can do inside of the "type" in the switch statement?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0 && self.videoInfoReceived == YES) {
        id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
        return [secInfo numberOfObjects] + 1;
    } else {
        id <NSFetchedResultsSectionInfo> secInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
        return [secInfo numberOfObjects];
    }

}

NSFetchedResultsController Delegates

NSFetchedResultsController Delegates

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
    [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];
            NSLog(@"didChangeSection - ChangeInsert");
            break;

        case NSFetchedResultsChangeDelete:
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            NSLog(@"didChangeSection - ChangeDelete");
            break;
    }
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{
    UITableView *tableView = self.tableView;

    switch(type) {
        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            NSLog(@"didChangeObject - ChangeInsert");
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            NSLog(@"didChangeObject - ChangeDelete");
            break;

        case NSFetchedResultsChangeUpdate:
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            NSLog(@"didChangeObject - ChangeUpdate");
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            NSLog(@"didChangeObject - ChangeMove");
            break;
    }
}

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


推荐答案

检查您发送的任何索引路径是否为第一个(零)段。如果是,则FRC被赋予错误行号,因为您已经告诉表视图一行计数,并且FRC理解了不同的行号。

You need more conditional code to check if any index path you're sent is for the first (zero) section. If it is, the FRC is being given the 'wrong' row number because you have told the table view one row count and the FRC understands a different one.

基本上,无论在哪里,您从表视图 接收到索引路径,请检查该部分,如果 == 0 ,创建具有相同部分的新索引路径和 row-1 ,并使用它来访问FRC中的对象。

Basically, everywhere you receive an index path from the table view, check the section, if it == 0, create a new index path with the same section and row - 1 and use that to access the objects in the FRC.

路径从FRC ,您需要 row + 1 才能使用它要求表视图插入/删除/移动。

Conversely, when you receive an index path from the FRC you need row + 1 before you can use it to ask the table view to insert/delete/move.

这篇关于更新由NSFetchedResultsController管理的UITableView,并在第一部分中有1个额外的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 03:22