首页 文章

iOS CoreData / NSFetchedResultsController:将结果限制为每节3个

提问于
浏览
1

想象一下新闻应用程序 . 该应用程序有多个 Channels ,每个 Channels 有x个新闻 . 第一个ViewController是一个UITableViewController,它将所有Channels显示为Sections和max . 每个 Channels /部分3个新闻 .

我使用NSFetchedResultsController . 我怎样才能实现NSFetchedResultsController每节获取3个新闻?

将NSFetchRequest中的fetchLimit设置为3会将整个结果限制为3(= 1行包含3行) .

任何的想法?

Edit

目前我使用此代码:
UITableViewDataSource 东西( NUMBER_OF_ITEMS_PER_CHANNEL = 3 ):

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

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

}

NSFetchedResultsControllerDelegate 东西:

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

- (void)controller:(NSFetchedResultsController*)controller
    didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo
             atIndex:(NSUInteger)sectionIndex
       forChangeType:(NSFetchedResultsChangeType)type
{
    UITableView* tableView = self.tableView;

    switch (type) {
    case NSFetchedResultsChangeInsert:
        [tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeDelete:
        [tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
        break;
    default:
        break;
    }
}

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

    switch (type) {
    case NSFetchedResultsChangeInsert:
        if (newIndexPath.row < NUMBER_OF_ITEMS_PER_CHANNEL) {
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            id<NSFetchedResultsSectionInfo> sectionInfo = [controller sections][newIndexPath.section];
            NSInteger numberOfRowsInSection = [sectionInfo numberOfObjects];
            if (numberOfRowsInSection > NUMBER_OF_ITEMS_PER_CHANNEL) {
                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:NUMBER_OF_ITEMS_PER_CHANNEL inSection:newIndexPath.section]] withRowAnimation:UITableViewRowAnimationFade];
            }
        }
        break;

    case NSFetchedResultsChangeDelete:
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:theIndexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeUpdate:
        [self fetchedResultsController:controller configureCell:[tableView cellForRowAtIndexPath:theIndexPath] atIndexPath:theIndexPath];
        break;

    case NSFetchedResultsChangeMove:
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:theIndexPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;
    }
}

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

但是当有新的新闻可用时(控制器:didChangeObject ....用 type = NSFetchedResultsChangeInsert 调用)我得到这样的错误:

CoreData:错误:严重的应用程序错误 . 在调用-controllerDidChangeContent:期间,从NSFetchedResultsController的委托中捕获到异常 . 更新无效:第3节中的行数无效 . 更新(3)后现有部分中包含的行数必须等于更新前该部分中包含的行数(3),加上或减去数字从该部分插入或删除的行(插入0,删除3)并加上或减去移入或移出该部分的行数(0移入,0移出) . 用户信息(null)

知道如何避免这个错误吗?

1 回答

  • 0

    我找到了 workaround 我的问题(这避免了CoreData错误) . 但我没有找到如何获得3个新闻PER Channels 的解决方案(仍然对那个感兴趣......) .

    如果你们中的一个有同样的问题 - 在这里我的解决方法:
    而不是删除节的最后一(第三)行并插入一个新的,我只是像这样更新单元格:

    - (void)controller:(NSFetchedResultsController*)controller
        didChangeObject:(id)anObject
            atIndexPath:(NSIndexPath*)theIndexPath
          forChangeType:(NSFetchedResultsChangeType)type
           newIndexPath:(NSIndexPath*)newIndexPath
    {
    
    ...
    
        switch (type) {
        case NSFetchedResultsChangeInsert:
            if (newIndexPath.row < self.numberOfNewsItemsToFetch) {
                id<NSFetchedResultsSectionInfo> sectionInfo = [controller sections][newIndexPath.section];
                NSInteger numberOfRowsInSection = [sectionInfo numberOfObjects];
                if (numberOfRowsInSection >= self.numberOfNewsItemsToFetch) {
                    NSIndexPath* indexPath = nil;
                    for (NSUInteger i = newIndexPath.row; i < self.numberOfNewsItemsToFetch; i++) {
                        indexPath = [NSIndexPath indexPathForRow:i inSection:newIndexPath.section];
                        [self fetchedResultsController:controller configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
                    }
                }
                else {
                    [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
                }
            }
            break;
    ...
        }
    }
    

相关问题