0

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

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

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

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

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

4

1 回答 1

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;
    }
}

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

于 2012-05-17T16:07:12.260 回答