如何给collectiontableview添加表头头

collectionview及其自定义
什么是UICollectionView?
UICollectionView是iOS6提供的一个新类,用来显示用户的序列化数据(ordered
data),也就是Pinterest的“瀑布流”,特别适合用来展示照片列表或杂志封面列表。
如何使用UICollectionView?
UICollectionView和UITableView非常相似:
UITableView有对应的UITableViewController、UITableViewCell、UITableViewDelegate、UITableViewDataSource,
UICollectionView有对应的UICollectionViewController、UICollectionViewCell、UICollectionViewDelegate、UICollectionViewDataSource;
UITableView和UICollectionView都是通过其delegate和datasource的一系列回调来获得其cell的外观和数据;
3 UITableView和UICollectionView都有cell复用机制;
UITableView和UICollectionView都有设置header和footer的方法。
同时两者又有不同之处:
UITableView的每行只能显示一个cell,而UICollectionView可以显示多个;
UICollection有专门控制布局管理的类(UICollectionViewLayout),而UITableView没有。
什么是UICollectionViewLayout?
假如没有UICollectionViewLayout,UICollectionView只能算UITableView的增强版罢了。
UICollectionViewLayout是一个抽象类,在使用UICollectionView时必须创建一个UICollectionViewLayout的子类,定义cell的位置等布局信息。当然我们并不是一定要自己定义这样一个子类,因为SDK中提供了一个现成的子类&UICollectionViewFlowLayout,很多情况下我们可以直接使用或者继承UICollectionViewFlowLayout。
有三种类型的元素需要layout:
cell:collection view中的主要元素,对应UITableView中的一个cell;
supplementary
view:可选的(optional)view,在界面上展示,但不可以被用户选择的元素,可用来实现header和footer;
decoration view:另一种supplementary view,装饰整个collection
view,一般用于背景。
每个layout都应当实现以下方法:
- (CGSize)collectionViewContentSize
返回一个collection view item的size
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
为所有item返回一个layout
attributes数组,数组中元素的类型为UICollectionViewLayoutAttributes。UICollectionViewLayoutAttributes记录了一个layout的位置、大小、透明度等信息。
注意,每一个可见的item都需要对应的layout attribute,包括cell、supplementary
view和decoration view
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath (可选)
为一个特定的item返回layout
attribute。注意,返回的attribute仅使用cell,不包括supplementary view和decoration
- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath(可选)
为一个特定的supplementary返回attribute(只在使用了supplementary
view时需要)。其中,kind是一个用来标识supplementary view种类的字符串。
- (UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)kind&atIndexPath:&(NSIndexPath *)indexPath(可选)
为一个特定的decoration返回attribute(只在使用了supplementary view时需要)。
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds(可选)
当bounds改变时,layout是否需要刷新。默认返回NO。
什么是UICollectionViewLayoutAttributes?
顾名思义,它提供了collectionview布局所需的各种属性,其中包括:
每个元素的尺寸和位置(size、center)
透明度(opacity)
变化矩阵(transform)
层次关系(zIndex)
什么是UICollectionViewFlowLayout?
iOS6中系统提供的layout。从名字中可以看出它是一种线性布局。事实上,它能够满足我们的绝大部分需要。使用UICollectionViewFlowLayout,我们可以方便地设置:
每个item的大小
item之间的间距
header和footer的大小
每个section距离四周的距离(Insets)
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。11:11 提问
collectionView修改标题的UILabel
每次用户点击collection view中的cell:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
需要修改一个标题中的label:
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath];
UILabel *headerTitle=(UILabel *)[headerView viewWithTag:1];
headerTitle.text=@"test";
每次tag都设置正确,但是内容并没改变,谢谢
按赞数排序
UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath];
if(!headerView)
headerView = [[UICollectionReusableView alloc] initwith.....];
UILabel *headerTitle=(UILabel *)[headerView viewWithTag:1];
headerTitle.text=@"test";
另外你的view的tag设置成根据indexPath 来确定.
其他相似问题
相关参考资料查看: 1276|回复: 0
最后登录15/2/12注册时间12/5/16阅读权限50积分-5精华0帖子金币-15618 枚91币0 枚
钻石: 0 颗
人气: 0 次
金币: -15618 个
发表于 15/5/29 12:22:58
ios中collection view的表头怎么设置
帖子永久地址:&<button type="submit" class="pn" onclick="setCopy('ios中collection view的表头怎么设置\n/thread--1.html', '帖子地址已经复制到剪贴板您可以用快捷键 Ctrl + V 粘贴到 QQ、MSN 里。')">推荐给好友
GMT+8, 16/4/25 14:12
Powered by

我要回帖

更多关于 ios collection的表头 的文章

 

随机推荐