如果你对UICollectionView并不熟悉,可以参考我的这篇文章
UICollectionView相信大家肯定不陌生了,类型和tableView很相似,但他比后者实力更为强大,之前很少写UICollectionView,抽个时间找了些例子,并自己写了个demo,作了个总结,现在记录一下基本用法,一方面便于自己遗忘查询,另一方面也希望帮到和我一样很少用到这种方法的童鞋。
遵循代理
UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
定义两个属性,分别是UICollectionView和数据源数组
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *arr;
cellID的定义,在这里是要注册cellID的,tableview中可以在cellforrow方法里面直接static 一个cellid,在这里不行。
// 注意const的位置
static NSString *const cellId = @"cellId";
static NSString *const headerId = @"headerId";
static NSString *const footerId = @"footerId";
基本方法
- (void)viewDidLoad {
[super viewDidLoad];
[self arr];
[self collectionView];
}
- (NSMutableArray *)arr {
if (_arr == nil) {
_arr = [[NSMutableArray alloc] initWithObjects:@"123",@"234",@"345",@"456",@"567",nil];
}
return _arr;
}
- (UICollectionView *)collectionView {
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
[layout setScrollDirection:UICollectionViewScrollDirectionVertical];
_collectionView = [[ UICollectionView alloc ] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 160) collectionViewLayout :layout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.backgroundColor = [UIColor grayColor];
[_collectionView registerClass :[ UICollectionViewCell class ] forCellWithReuseIdentifier : cellId ];
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerId];
[_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:footerId];
[self.view addSubview:_collectionView];
}
return _collectionView;
}
三个代理的代理方法,注释写在代码里了,有需要了解的小伙伴可以一步一步看
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return _arr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
UIButton * btn = [[UIButton alloc]initWithFrame:cell.bounds];
[btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
btn.backgroundColor = [UIColor yellowColor];
[btn setTitle:[_arr objectAtIndex:indexPath.row] forState:(UIControlStateNormal)];
[cell.contentView addSubview:btn];
btn.userInteractionEnabled = NO;
UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(press:)];
btn.userInteractionEnabled = YES;
[btn addGestureRecognizer:press];
// UICollectionViewCell *cell = [_collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
// cell.backgroundColor = [UIColor purpleColor];
return cell;
}
- (void)press:(UILongPressGestureRecognizer *)press {
if (press.state == UIGestureRecognizerStateBegan) {
NSLog(@"qwe");
}
}
// 和UITableView类似,UICollectionView也可设置段头段尾
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
UICollectionReusableView *headerView = [_collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerId forIndexPath:indexPath];
if (!headerView) {
headerView = [[UICollectionReusableView alloc] init];
}
headerView.backgroundColor = [UIColor yellowColor];
return headerView;
}
else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
UICollectionReusableView *footerView = [_collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:footerId forIndexPath:indexPath];
if (!footerView) {
footerView = [[UICollectionReusableView alloc] init];
}
footerView.backgroundColor = [UIColor lightGrayColor];
return footerView;
}
return nil;
}
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath
{
}
#pragma mark -- UICollectionViewDelegateFlowLayout
//定义每个item 的大小
- ( CGSize )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:( NSIndexPath *)indexPath{
return CGSizeMake ( 70 , 20 );
}
//定义边界item 的边距(次序: 上,左,下,右边)
-( UIEdgeInsets )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:( NSInteger )section{
return UIEdgeInsetsMake ( 10 , 10 , 10 , 10 );//最上面item距离上部10,最左边距离最左边边界10,其他也是这个意思,不清楚可以自己试试
}
//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
return 20;
}
//每个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
return 15;
}
//返回头headerView的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
CGSize size={320,45};
return size;
}
//返回尾footerView的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
CGSize size={320,45};
return size;
}
#pragma mark ---- UICollectionViewDelegate
//设置是否让item高亮(有时候我们点击item,让他变色,这里设置no是不会发生改变的)
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
// 点击高亮
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
cell.backgroundColor = [UIColor greenColor];
}
// 选中某item
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"选中了~~~");
}
// 长按某item,弹出copy和paste的菜单(以下三个方法要一起实现,这是实现copy和paste的,可以点进去看看苹果的注释)
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath
{
return NO; //在这里,为了实现自定义长按手势效应,我把这里长按弹出粘贴复制给禁用了,如果想看到效果,把上面collectionView的懒加载那里添加手势给去了,然后这里置为YES
}
// 使copy和paste有效
- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender
{
if ([NSStringFromSelector(action) isEqualToString:@"copy:"] || [NSStringFromSelector(action) isEqualToString:@"paste:"])
{
return YES;
}
return NO;
}
//执行对应操作
- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(nullable id)sender
{
if([NSStringFromSelector(action) isEqualToString:@"copy:"])
{
NSLog(@"-------------执行拷贝-------------");
//点击长按弹出copy和paste--点击copy删除cell
__weak typeof(self)weakSelf = self;
[_collectionView performBatchUpdates:^{
[weakSelf.arr removeObjectAtIndex:indexPath.row];
[weakSelf.collectionView deleteItemsAtIndexPaths:@[indexPath]];
//删除后再计算collection的高度
weakSelf.collectionView.frame = CGRectMake(0, 64, self.view.frame.size.width, 160);
} completion:nil];
}
else if([NSStringFromSelector(action) isEqualToString:@"paste:"])
{
NSLog(@"-------------执行粘贴-------------");
//点击长按弹出copy和paste--点击paste添加一个cell
__weak typeof(self)weakSelf = self;
[_collectionView performBatchUpdates:^{
// 构造一个indexPath
NSIndexPath *indePath = [NSIndexPath indexPathForItem:weakSelf.arr.count inSection:0];
[weakSelf.collectionView insertItemsAtIndexPaths:@[indePath]]; // 然后在此indexPath处插入给collectionView插入一个item
[weakSelf.arr addObject:@"110"]; // 保持collectionView的item和数据源一致
//增加后再计算collection的高度
weakSelf.collectionView.frame = CGRectMake(0, 64, self.view.frame.size.width, 160);
} completion:nil];
}
}
如果对您起到了帮助,就给我点个赞吧