ios - UItableView with MagicalRecord 海量数据集

标签 ios objective-c uitableview magicalrecord

我有一个存储一些数据的表。假设数据太大而无法一次全部加载到内存中。我想在 UItableView 中显示这些数据。

-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
   return 1;
}

-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   return  [Item MR_numberOfEntities];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


// How to get object for this row ???


return cell;

}

我知道的唯一方法是将所有数据加载到数组中

NSArray *items = [Item MR_findAll];

但我不想那样做。用户将显示前 10 行,为什么我应该从 CoreData 加载所有这些行。有什么方法可以使用 MagicalRecord 一个一个地获取它们吗?

最佳答案

根据 docs您需要初始化获取请求,并且您可能还想根据滚动进度设置获取偏移量。但您必须手动跟踪它。这是实现它的基本方法。 附言。我还没有测试过这段代码。我只是在文本编辑器中写的:)。但它应该按照你的要求工作。例如加载限制为 10 的项目。

    @property (nonatomic) int itemCount;

    @property (nonatomic, strong)  NSMutableArray * items 

    static const int FETCH_LIMIT = 10;


       -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
       {
        return  _itemCount;
       }

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            // Classic start method
            static NSString *cellIdentifier = @"MyCell";
            MyCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
            if (!cell)
            {
                cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MainMenuCellIdentifier];
            }

            MyData *data = [self.itemsArray objectAtIndex:indexPath.row];
            // Do your cell customisation
            // cell.titleLabel.text = data.title;

            if (indexPath.row == _itemCount - 1)
            {
                [self loadMoreItems];
            }
        }

    -(void)loadMoreItems{

      int newOffset = _itemCount+FETCH_LIMIT; // Or _itemCount+FETCH_LIMIT+1 not tested yet

      NSArray * newItems = [self requestWithOffset: newOffset];

     if(!newItems || newItems.count == 0){

        // Return nothing since All items have been fetched

       return;
     }

      [ _items addObjectsFromArray:newItems ];
    // Updating Item Count
      _itemCount = _items.count;

    // Updating TableView
       [tableView reloadData];
    }

    -(void)viewDidLoad{

     [super viewDidLoad];
          _items = [self requestWithOffset: 0];
          _itemCount =  items.count;
    }

    -(NSArray*)requestWithOffset: (int)offset{

          NSFetchRequest *itemRequest = [Item MR_requestAll];
          [itemRequest setFetchLimit:FETCH_LIMIT]
          [itemRequest setFetchOffset: offset]
          NSArray *items = [Item MR_executeFetchRequest:itemRequest];
       return items;
    } 

希望对您有所帮助:)

关于ios - UItableView with MagicalRecord 海量数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33527262/

相关文章:

objective-c - 可以在调用实例方法时释放 Objective-C 对象吗?

iphone - 当点击另一个 UITextField 调出选择器时,键盘不会关闭

ios - 让 UITableViewCell 使用自动布局调整自身大小

ios - 快速获取 iPod 库中的专辑列表

带键盘的 iOS PresentViewController

objective-c - UIScrollview 在缩小时更改 UIButton 的图像质量

ios - 如何在每个 UITableView 部分的底部插入一个按钮?

objective-c - 核心数据 - 不同的值

javascript - 通过在 WKWebView 中评估 JavaScript 来获取 HTML

ios - UITableView 滚动弹回顶部