iOS开发

测试MarkDown

2021-08-16  本文已影响0人  提笔挥墨

UICollectionView headerView 重用

1、必须注册并且签署delegate

1.1 3个代理
<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

[self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];

2、设置头视图大小

self.flow.headerReferenceSize = CGSizeMake(WIDTH, 60);

3、实现UICollectionViewDelegateFlowLayout 代理方法

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *headerView = nil;
    if (kind == UICollectionElementKindSectionHeader) {
        headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerView" forIndexPath:indexPath];
        // 避免重复添加
        if (headerView.subviews.count == 0) {
            headerView.backgroundColor = [UIColor redColor];
            UILabel *lab = [[UILabel alloc] init];
            [headerView addSubview:lab];
            lab.textColor = [UIColor blackColor];
            NSArray *arr = self.datas[indexPath.section];
            lab.backgroundColor = CLEAR_COLOR;
            lab.tag = 100;
            lab.text = arr[0];
            [lab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
            }];
        }

        UILabel *lab = [headerView viewWithTag:100];
        NSArray *arr = self.datas[indexPath.section];
        lab.text = arr[0];


    }
    return headerView;
}
上一篇下一篇

猜你喜欢

热点阅读