Swift学习iOS头条干货首页投稿(暂停使用,暂停投稿)

UICollectionView简单的分组实现

2016-06-22  本文已影响3762人  萌九

UICollectionView实现分组有很多种方式,我写的是一个简单的写法,思路你们可以看看,通过给UICollectionView注册头部视图,在获取用户点击的是几号头部,就在返回item个数的时候返回几号数组,数组和头部分区对应就行,再通过BOOL值判断一下isOpen就OK。
先看看效果图:

qh_2.gif

代码如下:

@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource> {
    NSInteger nowIndex; // 点击之后的tag值
}

@property (nonatomic, strong) NSArray *arr1;
@property (nonatomic, strong) NSArray *arr2;
@property (nonatomic, strong) NSArray *arr3;
@property (nonatomic, strong) NSMutableArray *dataArr;

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, assign) BOOL isOpen; // 判断时候点击分组

@end

@implementation ViewController
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(80, 80);
        layout.minimumLineSpacing = 10.0;
        layout.minimumInteritemSpacing = 0.0;
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
        layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 50);
        _collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor blueColor];
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
        _collectionView.showsVerticalScrollIndicator = YES;
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    }
    return _collectionView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"Collection分组";
    self.arr1 = [NSArray arrayWithObjects:@"1", @"2", nil];
    self.arr2 = [NSArray arrayWithObjects:@"222", @"333", nil];
    self.arr3 = [NSArray arrayWithObjects:@"33333", @"44444", nil];
    self.dataArr = [NSMutableArray arrayWithObjects:_arr1, _arr2, _arr3, nil].mutableCopy;
    _isOpen = NO; // 默认没有打开分组
    nowIndex = -1; // 
    [self.view addSubview:self.collectionView];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.dataArr.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == nowIndex) {
        // 判断打开的是哪个分区
        if (_isOpen) { // 如果打开分组
            return 0;
        }
        return [self.dataArr[nowIndex] count];
    }
    return 0;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
    UILabel *label = [[UILabel alloc] initWithFrame:cell.bounds];
    label.backgroundColor = [UIColor redColor];
    label.text = self.dataArr[indexPath.section][indexPath.row];
    [cell.contentView addSubview:label];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        header.backgroundColor = [UIColor cyanColor];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = header.bounds;
        [button setTitle:[NSString stringWithFormat:@"%ld", indexPath.section] forState:UIControlStateNormal];
        [button addTarget:self action:sel_registerName("doOpen:") forControlEvents:UIControlEventTouchUpInside];
        button.tag = 1000 + indexPath.section;
        for (UIView *view in header.subviews) {
            [view removeFromSuperview];
        } // 防止复用分区头
        [header addSubview:button];
        return header;
    } else {
        return nil;
    }
}
- (void)doOpen:(UIButton *)sender {
    if (nowIndex == sender.tag - 1000) {
        _isOpen = !_isOpen;
    } else {
        _isOpen = NO;
        nowIndex = sender.tag - 1000;
    }
    [_collectionView reloadData];
}

好了,分组实现就完成了,如果想用到项目中,要考虑数组的类型,或者item点击事件等。
配图:

qh_1.png
上一篇下一篇

猜你喜欢

热点阅读