【问题标题】:NSFetchedResultsController trying to limit number of records displayedNSFetchedResultsController 试图限制显示的记录数
【发布时间】:2012-05-16 13:36:18
【问题描述】:

在创建 NSFetchRequest 以传递给我的 NSFetchedResultsController 时,我将 fetchLimit 属性设置为 3。

现在最初这似乎工作正常。我可以以任何方式修改前三个返回的对象,从而改变它们的顺序并且它们都正确地重新洗牌。当我将最初落在前三个之外的对象更改为现在将其带入前三个时,或者只是添加一个新对象使其出现在前三个中时,就会出现问题。

我预期会发生什么:插入的对象将其余部分向下推,一个从底部掉落。

实际发生了什么:插入的对象将其余对象向下推,记录数增加到 4?!

谁能解释一下,或者我应该如何处理?

【问题讨论】:

  • 您是重新执行 fetch 还是只是听取更改?
  • 这听起来像是一个错误,也许考虑提交一个错误报告。
  • 只使用委托方法监听变化
  • 在插入和删除的情况下您是否尝试重新获取?
  • 也需要配合动作。但这违背了 NSFetchedResultsController 的目的,我不妨只保留一个数组。

标签: ios core-data nsfetchedresultscontroller nsfetchrequest


【解决方案1】:

我已经在解决这个问题上取得了一些进展,基本上是通过忽略 numberOfObjects 并返回我希望表格固定的实际长度。这在controller:didChangeObject:... 中需要一些技巧,但到目前为止似乎有效。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return kTableSize;
    //return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
}

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

    switch(type) {

        case NSFetchedResultsChangeInsert:

            // Only modify table if insert will effect visible rows
            if (newIndexPath.row < kTableSize) {
                // Delete last row to maintain fixed length
                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:(kTableSize - 1) inSection:newIndexPath.section]] withRowAnimation:UITableViewRowAnimationAutomatic];

                [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            }
            break;

        case NSFetchedResultsChangeDelete:

            // Only modify table if delete will effect visible rows
            if (indexPath.row < kTableSize) {
                [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

                // Insert extra row to maintain fixed length
                [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:(kTableSize - 1) inSection:newIndexPath.section]] withRowAnimation:UITableViewRowAnimationAutomatic];
            }
            break;

        case NSFetchedResultsChangeUpdate:

            // Only modify table if update will effect visible rows
            if (indexPath.row < kTableSize) {
                [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            }
            break;

        case NSFetchedResultsChangeMove:

            // Only modify table if move will effect visible rows
            if ((indexPath.row < kTableSize) || (newIndexPath.row < kTableSize)) {


                // Delete old row or last row of table
                if (indexPath.row < kTableSize) {
                    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                } else {
                    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:(kTableSize - 1) inSection:newIndexPath.section]] withRowAnimation:UITableViewRowAnimationAutomatic];
                }

                // Insert new row or a row at bottom of table
                if (newIndexPath.row < kTableSize) {
                    [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
                } else {
                    [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:(kTableSize - 1) inSection:newIndexPath.section]] withRowAnimation:UITableViewRowAnimationAutomatic];
                }
            }
            break;
    }
}

还需要注意tableView:cellForRowAtIndexPath:,以确保如果对象少于表长度,我们不会尝试访问不存在的对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多