您的位置:首页 > 产品设计 > UI/UE

UICollectionView 拾遗

2017-08-21 14:47 190 查看
1.如何给collectionView添加类似于tableview的 tableHeaderView

可以先给collectionView设置内边距 即insets  然后给collectionView添加子视图,注意子视图的frame的y是负值

2.如何给collectionView添加类似于tableview的 tableFooterView

可以先给collectionView设置内边距 即insets  然后给collectionView添加子视图,注意子视图的frame的y是

flowLayout.collectionViewContentSize.height

3.如何实现不同的section的head的高度不同

可以通过实现这个代理方法

UICollectionViewDelegateFlowLayout

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout
referenceSizeForHeaderInSection:(NSInteger)section {

    if (section ==0) {

        returnCGSizeMake(0,200);

    }

    else {

        returnCGSizeMake(0,100);

    }

}

如果head还有子视图,记得在DataSource的代理方法中重置子视图的frame

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString
*)kind atIndexPath:(NSIndexPath *)indexPath {

    // reusableView的宽和设置的headerReferenceSize设置的宽不相关

    UICollectionReusableView *reusableView = [collectionViewdequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:headerViewIDforIndexPath:indexPath];

    if (![reusableViewviewWithTag:10001]) {

        

        UILabel *serviceCategoryLab = [[UILabelalloc]initWithFrame:reusableView.bounds];

        serviceCategoryLab.backgroundColor = [UIColoryellowColor];

        serviceCategoryLab.tag =10001;

        [reusableView addSubview:serviceCategoryLab];

    }

    UILabel *serviceCategoryLab = (UILabel *)[reusableViewviewWithTag:10001];

    serviceCategoryLab.frame = reusableView.bounds;

    serviceCategoryLab.text =@"测试头分区";

    return reusableView;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: