NSFetchedResultsController试图限制显示的记录数

tra*_*per 0 core-data nsfetchedresultscontroller nsfetchrequest ios

创建一个NSFetchRequest传递给我的时候NSFetchedResultsController我将fetchLimit属性设置为3.

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

我期望发生的事情:插入的物体将其余部分向下推,一个从底部掉落.

实际发生的事情:插入的对象将其余部分向下推,记录数增加到4

任何人都可以解释这个,或者我应该如何解决这个问题?

tra*_*per 7

我已经取得了一些进展,基本上是通过忽略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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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