ios – 如何以编程方式在UICollectionVI中插入单元格?

我有一个UICollectionView,它的工作正常,但我想以编程方式将几个UICollectionViewCells项目添加到集合视图中.

那么我该如何实现呢?

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

解决方法

参考 UICollectionView documentation

You can accomplish:

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

  1. Update the data in your data source object.
  2. Call the appropriate method of the collection view to insert or delete the section or item.

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,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,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,or move items
within the same block.

从你的问题:你可以例如注册一个手势识别器,并插入一个新的单元格
做如下:

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

}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...