代码之家  ›  专栏  ›  技术社区  ›  user1904273

在ios中使用fetchlimit和核心数据的无限滚动tableview

  •  0
  • user1904273  · 技术社区  · 6 年前

    我在核心数据中有数千行数据。取这么大的数据会明显减慢用户体验。因为我让用户以不同的方式查看数据,重新加载是常见的,重新加载FRC可能会很痛苦。因此,我一直在我的取款上限是50英镑。50对于速度来说是很好的,但它只提取了一小部分行,因此旧数据根本无法访问。

    我想实现一个无限滚动,这样当用户点击tableview的底部时,就会加载新的行。我可以使用多个委托方法中的任何一个来触发重载,例如:

      - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
            NSInteger lastSectionIndex = [tableView numberOfSections] - 1;
            NSInteger lastRowIndex = [tableView numberOfRowsInSection:lastSectionIndex] - 1;
            if ((indexPath.section == lastSectionIndex) && (indexPath.row == lastRowIndex)) {
                // This is the last cell
                [self getMore];
            }
        }
    

    我的问题是用什么来得到更多。下面的方法很有效,但看起来很笨拙,因为需要重新加载所有先前的单元格,并且很难跟踪fetchlimit中的位置。有没有更有效的方法来完成无限卷轴?

       - (void)getMore {
            [self.fetchedResultsController.fetchRequest setFetchLimit:newFetchLimit];
           self.fetchedResultsController = nil;
            NSError *error;
            if (![self.fetchedResultsController performFetch:&error]) {
                // Update to handle the error appropriately.
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            }
    
            [self.tableView reloadData];
        }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   DeluxeAlonso    6 年前

    在您的GETOME函数中,您可以使用 fetchOffset 属性与FETCHEVELT相结合,这样您就可以创建任意结果集的子范围,并且避免再次加载所有的单元格内容。

    [fetchedResultsController.fetchRequest setFetchOffset: newFetchOffset];
    

    在哪里? newFetchOffset 将在0开始,在第二个请求时,您需要将其设置为50,这样您就可以得到下一个第五十个值。