怎样在UICollectionView中隐藏添加的footerviewHeader和footer

77178人阅读
iOS开发(24)
首先认识一下UICollectionView
NS_CLASS_AVAILABLE_IOS(6_0) @interface UICollectionView : UIScrollView
UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView 和 UITableViewController 类。
使用UICollectionView 必须实现UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout这三个协议。
下面给出一些常用方法,具体的使用可以参考Demo:& 苹果官方Demo:
- (void)viewDidLoad
[super viewDidLoad];
self.title = @&UICollectionView学习&;
//通过Nib生成cell,然后注册 Nib的view需要继承 UICollectionViewCell
[self.collectionView registerNib:[UINib nibWithNibName:@&SQCollectionCell& bundle:nil] forCellWithReuseIdentifier:kcellIdentifier];
//注册headerView Nib的view需要继承UICollectionReusableView
[self.collectionView registerNib:[UINib nibWithNibName:@&SQSupplementaryView& bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kheaderIdentifier];
//注册footerView Nib的view需要继承UICollectionReusableView
[self.collectionView registerNib:[UINib nibWithNibName:@&SQSupplementaryView& bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kfooterIdentifier];
self.collectionView.allowsMultipleSelection = YES;//默认为NO,是否可以多选
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
#pragma mark -CollectionView datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
//item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
//重用cell
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kcellIdentifier forIndexPath:indexPath];
UIImageView *imageView = (UIImageView *)[cell viewWithTag:1];
UILabel *label = (UILabel *)[cell viewWithTag:2];
NSString *imageName = [NSString stringWithFormat:@&%ld.JPG&,(long)indexPath.row];
imageView.image = [UIImage imageNamed:imageName];
label.text = imageN
cell.backgroundColor = [UIColor redColor];
// The view that is returned must be retrieved from a call to -dequeueReusableSupplementaryViewOfKind:withReuseIdentifier:forIndexPath:
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
NSString *reuseI
if ([kind isEqualToString: UICollectionElementKindSectionFooter ]){
reuseIdentifier = kfooterI
reuseIdentifier = kheaderI
UICollectionReusableView *view =
[collectionView dequeueReusableSupplementaryViewOfKind :kind
withReuseIdentifier:reuseIdentifier
forIndexPath:indexPath];
UILabel *label = (UILabel *)[view viewWithTag:1];
if ([kind isEqualToString:UICollectionElementKindSectionHeader]){
label.text = [NSString stringWithFormat:@&这是header:%d&,indexPath.section];
else if ([kind isEqualToString:UICollectionElementKindSectionFooter]){
view.backgroundColor = [UIColor lightGrayColor];
label.text = [NSString stringWithFormat:@&这是footer:%d&,indexPath.section];
//定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
return CGSizeMake(60, 80);
//定义每个Section 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
return UIEdgeInsetsMake(15, 15, 5, 15);//分别为上、左、下、右
//返回头headerView的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
CGSize size={320,45};
//返回头footerView的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
CGSize size={320,45};
//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
return 10;
//每个item之间的间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
return 100;
//选择了某个cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
[cell setBackgroundColor:[UIColor greenColor]];
//取消选择了某个cell
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
[cell setBackgroundColor:[UIColor redColor]];
效果图如下:
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:200036次
积分:1117
积分:1117
排名:千里之外
转载:27篇
评论:19条
(2)(2)(1)(1)(2)(3)(2)(1)(6)(15)Pages: 1/2
主题 : 如何纯代码给UICollectionView添加HeaderView
级别: 新手上路
可可豆: 56 CB
威望: 100 点
在线时间: 28(时)
发自: Web Page
如何纯代码给UICollectionView添加HeaderView&&&
在ib里,可以通过属性设置来给UICollectionView添加Header或者Footer
&
但是当我尝试使用代码创建添加Header的时候,没有找到相关的属性和方法。
&
百度无用,谷歌无用。特来请教大神们。求赐教。
级别: 新手上路
可可豆: 96 CB
威望: 96 点
在线时间: 86(时)
发自: Web Page
我遇到和楼主同样的问题,同求。
级别: 新手上路
UID: 216821
可可豆: 193 CB
威望: 72 点
在线时间: 342(时)
发自: Web Page
http://my.oschina.net/u/723760/blog/221525参考这个,我是跟着这个学的,然后自己再琢磨
级别: 侠客
UID: 155386
可可豆: 301 CB
威望: 394 点
在线时间: 236(时)
发自: Web Page
[layout setHeaderReferenceSize:CGSizeMake(320, 50)];
级别: 新手上路
可可豆: 56 CB
威望: 100 点
在线时间: 28(时)
发自: Web Page
问题已经解决
级别: 新手上路
可可豆: 77 CB
威望: 74 点
在线时间: 206(时)
发自: Web Page
楼主有答案吗!可否分享下!
级别: 新手上路
可可豆: 77 CB
威望: 74 点
在线时间: 206(时)
发自: Web Page
楼主有答案吗!可否分享下!
级别: 新手上路
可可豆: 76 CB
威望: 27 点
在线时间: 135(时)
发自: Web Page
可以试下。-(CGSize)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{    if(section == 0)    {        CGSize size = {320, 150};            }    else    {        CGSize size = {320, 50};            }}
级别: 侠客
UID: 468217
可可豆: 205 CB
威望: 190 点
在线时间: 118(时)
发自: Web Page
楼主无良呀,自己解决了又不坑声...
2015转型者,时年31.而立之年被生活迫而转。
级别: 新手上路
可可豆: 1 CB
威望: 1 点
在线时间: 14(时)
发自: Web Page
从 UICollectionReusableView 继承。 如 @interface HeaderCRView : UICollectionReusableView然后在Vc里面通过registerClass把该类注册进去 如 [self.collectionView registerClass:[HeaderCRView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kheaderIdentifier];当然这个设置不能忘记-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{&&&&CGSize size={320,45};&&&&}[ 此帖被hzzqh在 10:00重新编辑 ]
Pages: 1/2
关注本帖(如果有新回复会站内信通知您)
4*5+2 正确答案:22
发帖、回帖都会得到可观的积分奖励。
按"Ctrl+Enter"直接提交
关注CocoaChina
关注微信 每日推荐
扫一扫 浏览移动版怎样在UICollectionView中添加Header和footer_百度知道
怎样在UICollectionView中添加Header和footer
提问者采纳
- (NSInteger)numberOfSectionsInCollectionVIew:(UICollectionView *)collectionView{
return [recipeImages count]。
最后,怎样给collection view实现两个section首先检索该数组的section number然后从section中获取具体的items.m中的numberOfSectionsInCollectionView方法:在RecipeCollectionViewController,这个可以通过方法调用下面的方法来完成即,在collectin View中返回section中的数量
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁怎样在UICollectionView中添加Header和footer_百度知道
怎样在UICollectionView中添加Header和footer
提问者采纳
在Storyboard中设计Header和Footer
首先download the header/footer background images并且添加到Xcode工程中。
到Storyboard中,选择collection view controller中的&Collection View&。在Attributes inspector中,选择&Section Header&和&Section Footer&。
在header和footer之间默认为空,我们会用storyboard来设计视图。header view是专门用来显示一个部分的标题,而底部视图只显示静态横幅图片。利用storyboard,从对象库中拖出image view并在其上面添加一个标签。设置字体颜色为白色,底部视图只需添加一个image view
选中footer view中的image view,在Attributes inspector中命名背景图片为&footer_banner.png&。最重要的是,我们必须为header和footer view指定一个标识符。这个标示符将会被用于代码识别图片名称。在Atteributes inspecto供顶垛雇艹概讹谁番京r中设置header view的identifier为“HeaderView”,同样的把footer view的identifier设置为“FooterView”。
工程信息部总经理
其他类似问题
为您推荐:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁&&&&iOS UICollectionView 纯代码布局,添加Section Header 头部视图跟 Section Footer尾部视图
iOS UICollectionView 纯代码实现布局,以及纯代码实现Section header 头视图,以及 Section footer 尾部视图。纯代码,无xib
若举报审核通过,可奖励20下载分
被举报人:
举报的资源分:
请选择类型
资源无法下载
资源无法使用
标题与实际内容不符
含有危害国家安全内容
含有反动色情等内容
含广告内容
版权问题,侵犯个人或公司的版权
*详细原因:
VIP下载&&免积分60元/年(1200次)
您可能还需要
移动开发下载排行}

我要回帖

更多关于 scrollview添加header 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信