码迷,mamicode.com
首页 > 其他好文 > 详细

UICollection类似TableView的标题

时间:2016-03-11 15:23:57      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:

记得第一次写CollectionView标题的时候,查了很多资料结果发现都不是很完整,经过努力还是做出来了现在跟大家分享下,让还在坑里的小伙伴快速出坑废话少说直接开始

在创建好collection的情况下我们开始第一步

首先创建一个继承与UICollectionReusableView的类

然后在collectionView注册头部视图

static NSString *kheaderIdentifier = @"headerIdentifier";
[collectionViews registerClass:[YYClassReusable class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kheaderIdentifier];
YYClassReusable是继承与UICollectionReusableView的类----继续下一步定义标题头部的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    CGSize size = CGSizeMake([[UIScreen mainScreen] bounds].size.width, 60);
    return size;
}

最后一步在UICollectionViewDataSource代理有个方法

#pragma mark -- 设置头尾部内容
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = nil;
#pragma mark -- 定制头部视图的内容
    if (kind == UICollectionElementKindSectionHeader) {
        YYClassReusable *headerV = (YYClassReusable *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kheaderIdentifier forIndexPath:indexPath];
        if (indexPath.section == 0) { 
            [headerV setTitle:@"我是标题1"];
        }else {
            [headerV setTitle:@"我是标题2"];
        }
        reusableView = headerV;
    }
    return reusableView;
}

基本就是上面这三个步骤,就可以完成了,,如果有什么需要补充的可以@我大家互相探讨

最后老规矩附上demo----http://pan.baidu.com/s/1eRmQamI

 

 

 

UICollection类似TableView的标题

标签:

原文地址:http://www.cnblogs.com/pengmlop/p/5265687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!