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

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

问题描述

我正在尝试向现有的 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天全站免登陆