Muh*_*sir 21 objective-c collectionview ios
我有一个UICollectionView
它工作正常,但我想以UICollectionViewCells
编程方式将一些项添加到集合视图中.
那我该怎么做呢?
进一步澄清:当我以编程方式说我的意思是在运行时插入一个单元格,当一个动作被触发时,而不是在加载应用程序时(使用该viewDidLoad
方法).我知道什么时候该模型被更新,调用是由UICollectionView
在insertItemsAtIndexPaths:
法.它应该创建一个新的单元格,但它没有这样做,它抛出一个错误.
abd*_*una 24
...通过引用UICollectionView文档
你可以完成:
插入,删除和移动节和项要插入,删除或移动单个节或项,请按照下列步骤操作:
- 更新数据源对象中的数据.
- 调用集合视图的相应方法以插入或删除节或项.
在通知集合视图任何更改之前更新数据源至关重要.集合视图方法假定您的数据源包含当前正确的数据.如果没有,集合视图可能会从您的数据源收到错误的项目集,或者询问不存在的项目并使您的应用程序崩溃.当您以编程方式添加,删除或移动单个项目时,集合视图的方法会自动创建动画以反映更改.但是,如果要一起为多个更改设置动画,则必须在块内执行所有插入,删除或移动调用,并将该块传递给performBatchUpdates:completion:方法.然后,批量更新过程会同时为您的所有更改设置动画,您可以自由地将调用混合到插入,删除,
从您的问题:您可以通过执行以下操作注册A手势识别器和插入新手机:
在
// 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];
}
Run Code Online (Sandbox Code Playgroud)
Ani*_*ese 12
如果要插入多items
到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]];
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
57731 次 |
最近记录: |