ios 如何以编程方式在 UICollectionVIew 中插入单元格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15572462/
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
How to insert Cell in UICollectionVIew Programmatically?
提问by Muhammad Nasir
I have a UICollectionView
and it works fine, but I want to add a few UICollectionViewCells
items programmatically into the collection view.
我有一个UICollectionView
并且它工作正常,但我想以UICollectionViewCells
编程方式将一些项目添加到集合视图中。
So how can I achieve this?
那么我怎样才能做到这一点呢?
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.
进一步澄清:当我以编程方式说时,我的意思是在运行时插入一个单元格,当一个动作被触发时,而不是在应用程序加载时(使用该viewDidLoad
方法)。我知道什么时候该模型被更新,调用是由UICollectionView
在insertItemsAtIndexPaths:
法。它应该创建一个新单元格,但它没有这样做,它抛出了一个错误。
回答by abdimuna
...By referring to UICollectionView documentation
...通过参考UICollectionView 文档
You can accomplish:
Inserting, Deleting, and Moving Sections and Items To insert, delete, or move a single section or item, follow these steps:
- Update the data in your data source object.
- 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, 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.
您可以实现:
插入、删除和移动部分和项目要插入、删除或移动单个部分或项目,请按照下列步骤操作:
- 更新数据源对象中的数据。
- 调用集合视图的适当方法来插入或删除部分或项目。
在通知集合视图任何更改之前更新数据源至关重要。集合视图方法假定您的数据源包含当前正确的数据。如果没有,集合视图可能会从您的数据源接收错误的项目集,或者请求不存在的项目并导致您的应用程序崩溃。当您以编程方式添加、删除或移动单个项目时,集合视图的方法会自动创建动画以反映更改。但是,如果您想同时为多个更改设置动画,则必须在块内执行所有插入、删除或移动调用,并将该块传递给 performBatchUpdates:completion: 方法。批量更新过程然后同时为您的所有更改设置动画,您可以自由混合调用插入、删除、
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];
}
回答by Anil Varghese
If you are inserting multiple items
into UICollectionView
, you can use performBatchUpdates:
如果要插入多个items
into UICollectionView
,则可以使用performBatchUpdates:
[self.collectionView performBatchUpdates:^{
// Insert the cut/copy items into data source as well as collection view
for (id item in self.selectedItems) {
// update your data source array
[self.images insertObject:item atIndex:indexPath.row];
[self.collectionView insertItemsAtIndexPaths:
[NSArray arrayWithObject:indexPath]];
}
}
回答by Oleg
– insertItemsAtIndexPaths:
does the job
– insertItemsAtIndexPaths:
做这份工作
回答by Curnelious
Here is how to insert an item in Swift 3 :
以下是在 Swift 3 中插入项目的方法:
let indexPath = IndexPath(row:index, section: 0) //at some index
self.collectionView.insertItems(at: [indexPath])
You must first update your data.
您必须首先更新您的数据。