UICollectionView簡(jiǎn)單的分組實(shí)現(xiàn)

UICollectionView實(shí)現(xiàn)分組有很多種方式,我寫的是一個(gè)簡(jiǎn)單的寫法,思路你們可以看看,通過給UICollectionView注冊(cè)頭部視圖,在獲取用戶點(diǎn)擊的是幾號(hào)頭部,就在返回item個(gè)數(shù)的時(shí)候返回幾號(hào)數(shù)組,數(shù)組和頭部分區(qū)對(duì)應(yīng)就行,再通過BOOL值判斷一下isOpen就OK。
先看看效果圖:

qh_2.gif

代碼如下:

@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource> {
    NSInteger nowIndex; // 點(diǎn)擊之后的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; // 判斷時(shí)候點(diǎn)擊分組

@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; // 默認(rèn)沒有打開分組
    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) {
        // 判斷打開的是哪個(gè)分區(qū)
        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];
        } // 防止復(fù)用分區(qū)頭
        [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];
}

好了,分組實(shí)現(xiàn)就完成了,如果想用到項(xiàng)目中,要考慮數(shù)組的類型,或者item點(diǎn)擊事件等。
配圖:

qh_1.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容