将更多UICollectionViewCell添加到现有的UICollectionView [英] Add more UICollectionViewCell to an existing UICollectionView

查看:98
本文介绍了将更多UICollectionViewCell添加到现有的UICollectionView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将更多单元格添加到现有的 UICollectionView 中,该单元格已经填充了一些单元格。

I'm trying to add some more cells to an existing UICollectionView, which is already filled with some cells.

我试图使用CollectionView reloadData 但它似乎重新加载整个collectionView而我只是想添加更多细胞。

I tried to use the CollectionView reloadData but it seems to reload the entire collectionView and I just wanted to add more cells.

有人可以帮帮我吗?

推荐答案

最简单的插入方法 UICollectionView 的新单元格,无需重新加载所有单元格,只需使用 performBatchUpdates ,这可以通过以下步骤轻松完成。

The easiest way to insert new cells to the UICollectionView without having to reload all its cell is by using the performBatchUpdates, which can be done easily by following the steps below.

// Lets assume you have some data coming from a NSURLConnection
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *erro)
{
      // Parse the data to Json
      NSMutableArray *newJson = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

      // Variable used to say at which position you want to add the cells
      int index;

      // If you want to start adding before the previous content, like new Tweets on twitter
      index = 0;

      // If you want to start adding after the previous content, like reading older tweets on twitter
      index = self.json.count;

      // Create the indexes with a loop
      NSMutableArray *indexes = [NSMutableArray array];

      for (int i = index; i < json.count; i++)
      {
            [indexes addObject:[NSIndexPath indexPathForItem:i inSection:0]];
      }

      // Perform the updates
      [self.collectionView performBatchUpdates:^{

           //Insert the new data to your current data
           [self.json addObjectsFromArray:newJson];

           //Inser the new cells
           [self.collectionView insertItemsAtIndexPaths:indexes];

      } completion:nil];
 }

这篇关于将更多UICollectionViewCell添加到现有的UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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