如何以编程方式在UICollectionVIew中插入Cell? [英] How to insert Cell in UICollectionVIew Programmatically?

查看:87
本文介绍了如何以编程方式在UICollectionVIew中插入Cell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UICollectionView ,它工作正常,但我想以编程方式将几个 UICollectionViewCells 项添加到收藏视图。

I have a UICollectionView and it works fine, but I want to add a few UICollectionViewCells items programmatically into the collection view.

那么我怎样才能做到这一点?

So how can I achieve this?

进一步澄清:当我以编程方式说我的意思在运行时,触发操作时插入单元格,而不是在加载应用程序时插入单元格(使用 viewDidLoad 方法)。我知道模型何时更新,并且在 insertItemsAtIndexPaths:方法中调用 UICollectionView 。它应该创建一个新的单元格,但它不会这样做,它会抛出错误。

To further clarify: when I say programmatically I mean inserting a cell during runtime, when an action is fired, not when the app is loaded (using the viewDidLoad method). I know when the model is updated and the call is made to UICollectionView in the insertItemsAtIndexPaths: method. It should create a new cells, but it's not doing that, it's throwing an error.

推荐答案

...通过引用< a href =https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionView_class/\"rel =noreferrer> UICollectionView文档


您可以完成:

You can accomplish:

插入,删除和移动章节和项目要插入,删除,
或移动单个部分或项目,请按照下列步骤操作:

Inserting, Deleting, and Moving Sections and Items To insert, delete, or move a single section or item, follow these steps:


  1. 更新数据源对象中的数据。

  2. 调用集合视图的相应方法以插入或删除部分或项目。

更新数据至关重要在通知任何更改的
集合视图之前的来源。集合视图方法假设您的数据源包含当前正确的数据
。如果没有
,则集合视图可能会从
您的数据源收到错误的项目集,或者询问不存在的项目并使您的
应用程序崩溃。当您以编程方式添加,删除或移动单个项目时,
集合视图的方法会自动创建动画以反映
更改。但是,如果要一起为多个更改设置动画,则必须在块内执行所有插入,删除或移动调用,并且
将该块传递给performBatchUpdates:completion:方法。
批量更新过程然后在相同的
时间动画您的所有更改,您可以自由地混合调用以在同一块中插入,删除或移动项目

It is critical that you update your data source before notifying the collection view of any changes. The collection view methods assume that your data source contains the currently correct data. If it does not, the collection view might receive the wrong set of items from your data source or ask for items that are not there and crash your app. When you add, delete, or move a single item programmatically, the collection view’s methods automatically create animations to reflect the changes. If you want to animate multiple changes together, though, you must perform all insert, delete, or move calls inside a block and pass that block to the performBatchUpdates:completion: method. The batch update process then animates all of your changes at the same time and you can freely mix calls to insert, delete, or move items within the same block.

来自您的问题:您可以注册一个手势识别器,并按
插入一个新单元格,执行以下操作:

From your Question: you can for example register A gesture Recognizer, and Insert a NEW cell by doing the following:

in

// in .h 
@property (nonatomic, strong) NSMutableArray *data;

// in .m 
@synthesize data
// 

- (void)ViewDidLoad{
      //.... 

    myCollectonView.dataSource = self;
    myCollectionView.delegate = self; 
    data = [[NSMutableArray alloc] initWithObjects:@"0",@"1", @"2" @"3", @"4", 
                                                   @"5",@"6", @"7",  @"8", @"9",  
                                                   @"10", @"11", @"12", @"13",
                                                   @"14", @"15", nil];

    UISwipeGestureRecognizer *swipeDown = 
     [[UISwipeGestureRecognizer alloc] 
       initWithTarget:self action:@selector(addNewCell:)];
    swipeDown.direction = UISwipeGestureRecognizerDirectionDown;
        [self.view addGestureRecognizer:swipeDown];
    //.. 
} 


-(void)addNewCell:(UISwipeGestureRecognizer *)downGesture {
    NSArray *newData = [[NSArray alloc] initWithObjects:@"otherData", nil];
    [self.myCollectionView performBatchUpdates:^{
        int resultsSize = [self.data count]; //data is the previous array of data
        [self.data addObjectsFromArray:newData];
        NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];

        for (int i = resultsSize; i < resultsSize + newData.count; i++) {
            [arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:i 
                                                              inSection:0]];
        }
        [self.myCollectionView insertItemsAtIndexPaths:arrayWithIndexPaths];
    } completion:nil];

}

这篇关于如何以编程方式在UICollectionVIew中插入Cell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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