在iOS开发过程中,UICollectionView的使用仅次于UITableView的使用,而且UICollectionView的使用也很重要,需要很好的运用掌握相关知识点。本篇博文就来讲解一下UICollectionView使用中,删除UICollectionViewCell的步骤方法,仅供参考。
使用场景:在UICollectionView的cell中,点击长按手势删除列表中的其中一个UICollectionViewCell的方法。具体实现步骤如下所示。
声明一个全局的可变数组属性,并初始化这个可变数组。
//声明属性
@property (strong, nonatomic) NSMutableArray *dateSource;
//初始化可变数组
self.dateSource = [NSMutableArray new];
//给可变数组赋值
NSArray *customArr = @[@"95",@"95",@"90",@"90",@"95",@"85"];
_dateSource = [NSMutableArray arrayWithArray:customArr];
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:selfaction: @selector(handleLongPress:)];
longPressRecognizer.minimumPressDuration = 1.0f; // 触发长按事件时间为1.0秒
longPressRecognizer.delegate = self;
longPressRecognizer.view.tag = indexPath.row;
[cell addGestureRecognizer:longPressRecognizer];
@property (strong, nonatomic) NSIndexPath *selectedIndexPath; //cell点击标记
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
[collectionView deselectItemAtIndexPath:indexPath animated:YES];
_selectedIndexPath = indexPath; // 赋值
[_collectionView reloadData];
}
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
if (_customArr.count == 0) {
return;
}
[_dateSource removeAllObjects];
[_dateSource removeObjectAtIndex:recognizer.view.tag]; //删除数据源
[_collectionView deleteItemsAtIndexPaths:@[indexPath]]; //删除操作
[_collectionView reloadData]; //刷新
}else if (recognizer.state == UIGestureRecognizerStateEnded) {
NSLog(@"结束触发长按操作");
}
}
通过以上的步骤,大概演绎了一个简单的删除UICollectionViewCell的方法,如有疑问随时交流。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。