gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 08:02:46 28 4
gpt4 key购买 nike

我有一个 UICollectionView,它工作正常,但我想以编程方式将一些 UICollectionViewCells 项添加到 Collection View 中。

那么我该如何实现呢?

进一步澄清:当我以编程方式说时,我的意思是在运行时插入一个单元格,当一个 Action 被触发时,而不是在应用程序加载时(使用 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, 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.

根据您的问题:例如,您可以注册一个手势识别器,然后插入一个新单元格执行以下操作:

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

}

关于ios - 如何以编程方式在 UICollectionView 中插入单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15572462/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com