xcode 将更多 UICollectionViewCell 添加到现有 UICollectionView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13184098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:59:23  来源:igfitidea点击:

Add more UICollectionViewCell to an existing UICollectionView

xcodeios6uicollectionviewuicollectionviewcell

提问by Anderson Bressane

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

我正在尝试向UICollectionView已填充一些单元格的现有 中添加更多单元格。

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

我尝试使用 CollectionView,reloadData但它似乎重新加载了整个 collectionView,我只想添加更多单元格。

Can anybody help me?

有谁能够帮助我?

采纳答案by Anderson Bressane

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

将新单元格插入UICollectionView而无需重新加载其所有单元格的最简单方法是使用performBatchUpdates,可以按照以下步骤轻松完成。

// 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];
 }

回答by chris

The UICollectionViewclass has methods to add/remove items. E.g., to insert an item at some index(in section 0), modify your model accordingly and then do:

UICollectionView类有方法来添加/删除项目。例如,要在某处index(在 section 中0)插入一个项目,请相应地修改您的模型,然后执行以下操作:

int indexPath = [NSIndexPath indexPathForItem:index];
NSArray *indexPaths = [NSArray arrayWithObject:indexPath inSection:0];
[collectionView insertItemsAtIndexPaths:indexPaths];

The view will do the rest.

视图将完成剩下的工作。