NSFetchedResultsController试图限制显示的记录数 [英] NSFetchedResultsController trying to limit number of records displayed

查看:140
本文介绍了NSFetchedResultsController试图限制显示的记录数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

现在开始看起来工作正常。我可以修改前三个返回的对象,以任何方式改变它们的顺序,他们都正确地重新洗牌。问题出现时,我改变一个对象,最初落在前三个以外的方式,现在将其带入前三个,或者当简单地添加一个新的对象,因此它将出现在前三个。



我预期会发生什么:插入的对象将其余的按下,一个从底部拖出。



实际上发生了:插入的对象将其余部分按下,并且记录计数增加到4



任何人都可以解释这个问题,这个?

解决方案

我已经取得了一些进展,基本上通过忽略 numberOfObjects 并返回我想要的表的固定长度的实际长度。这在 controller:didChangeObject:... 中有点麻烦,但似乎工作到目前为止。

   - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
return kTableSize;
// return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects]; (NSFetchedResultsChangeType)类型newIndexPath:(NSIndexPath *)newIndexPath(NSIndexPath *)newIndexPath(NSIndexPath *)newIndexPath(NSIndexPath *)newIndexPath(NSFetchedResultsController *)控制器didChangeObject:
{
UITableView * tableView = self.myTableView;

switch(type){

case NSFetchedResultsChangeInsert:

//只有在insert会影响可见行时才修改表
if(newIndexPath .row< kTableSize){
//删除最后一行以保持固定长度
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:(kTableSize - 1)inSection:newIndexPath.section]] withRowAnimation: UITableViewRowAnimationAutomatic];

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

case NSFetchedResultsChangeDelete:

//只有在删除会影响可见行时才修改表
if(indexPath.row< kTableSize){
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

//插入额外的行以保持固定长度
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:(kTableSize - 1)inSection:newIndexPath.section]] withRowAnimation:UITableViewRowAnimationAutomatic];
}
break;

case NSFetchedResultsChangeUpdate:

//如果更新会影响可见行,则只修改表
if(indexPath.row< kTableSize){
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
break;

case NSFetchedResultsChangeMove:

//只有在移动会影响可见行时才修改表
if((indexPath.row< kTableSize)||(newIndexPath.row < kTableSize)){


//删除表的旧行或最后一行
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];
}

//在表的底部插入新行或行
if(newIndexPath.row [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject :newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
} else {
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:(kTableSize - 1)inSection:newIndexPath.section]] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
break;
}
}

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


When creating a NSFetchRequest to pass over to my NSFetchedResultsController I am setting the fetchLimit property to 3.

Now initially this appears to work fine. I can modify the first three returned objects in any way which changes their order and they all reshuffle correctly. The problem appears when I either alter a object which initially fell outside the first three in a way which now brings it into the first three, or when simply adding a new object so it will appear within the first three.

What I expected to happen: Inserted object pushes the rest down and one drops off the bottom.

What actually happens: Inserted object pushes the rest down and the record count grows to 4?!

Can anyone explain this, or how I should deal with this?

解决方案

I have made some progress working around this, basically by ignoring numberOfObjects and returning the actual length I want the table fixed at. This takes a bit of trickery in controller:didChangeObject:... but seems to be working so far.

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

Also need to take care in tableView:cellForRowAtIndexPath: to make sure we don't try and access an object which doesn't exist if there are less objects than the table length.

这篇关于NSFetchedResultsController试图限制显示的记录数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆