UICollectionView添加headerview/footerView

下面以创建footerview为例:


1、在UICollectionViewFlowLayout中添加:

flowLayout.footerReferenceSize =CGSizeMake(ScreenWidth, 30);

2、创建自定义的Footerview,继承于UICollectionReusableView ,这里命名为:"MyFooterView"

3、注册这个类

[_collectionViewregisterClass:[MyFooterViewclass]forSupplementaryViewOfKind:UICollectionElementKindSectionFooterwithReuseIdentifier:@"footer"];

4、添加方法

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kindisEqualToString:UICollectionElementKindSectionFooter]) {
        MyFooterView *footerView = [collectionViewdequeueReusableSupplementaryViewOfKind:kindwithReuseIdentifier:@"footer"forIndexPath:indexPath];
        return footerView;
    }
    returnnil;
}




发布了93 篇原创文章 · 获赞 21 · 访问量 25万+
展开阅读全文

同一界面下 两个collectionView 如何使用 求大神解答

11-12

我想在一个controller下做两个不section 和cell 数不同的 collectionView 请大神指教 给跪了!!! #import "ViewController.h" #define Screen_Width [UIScreen mainScreen].bounds.size.width //获取屏幕高度 #define Screen_Height [UIScreen mainScreen].bounds.size.height #define left 1001 #define right 1002 @interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout> @property (strong, nonatomic)UICollectionView *collectionViewLeft; @property (strong, nonatomic)UICollectionView *collectionViewRight; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self initUI]; } -(void)initUI { UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init]; // [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical]; [flowLayout setHeaderReferenceSize:CGSizeMake(Screen_Width/2, 50)]; self.collectionViewLeft=[[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, Screen_Width/2, Screen_Height-64) collectionViewLayout:flowLayout]; self.collectionViewLeft.dataSource=self; self.collectionViewLeft.delegate=self; self.collectionViewLeft.tag=left; [self.collectionViewLeft setBackgroundColor:[UIColor blackColor]]; //注册Cell,必须要有 [self.collectionViewLeft registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"]; //注册headView,有headView必须有注册 [_collectionViewLeft registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"]; [self.view addSubview:self.collectionViewLeft]; self.collectionViewRight=[[UICollectionView alloc] initWithFrame:CGRectMake(Screen_Width/2, 20, Screen_Width/2, Screen_Height-20) collectionViewLayout:flowLayout]; self.collectionViewRight.dataSource=self; self.collectionViewRight.delegate=self; self.collectionViewRight.tag=right; [self.collectionViewRight setBackgroundColor:[UIColor blackColor]]; //注册Cell,必须要有 [self.collectionViewRight registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"]; //注册headView,有headView必须有注册 [_collectionViewRight registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"]; [self.view addSubview:self.collectionViewRight]; } #pragma mark -- UICollectionViewDataSource //定义展示的UICollectionViewCell的个数 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (collectionView.tag==left) { return 3; }else{ return 2; } } //定义展示的Section的个数 -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { if (collectionView.tag==left) { return 5; }else{ return 3; } } //每个UICollectionView展示的内容 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString * CellIdentifier = @"UICollectionViewCell"; UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; cell.backgroundColor = [UIColor redColor]; for (id subView in cell.contentView.subviews) { [subView removeFromSuperview]; } return cell; } #pragma mark --UICollectionViewDelegateFlowLayout //定义每个Item 的大小 - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { return CGSizeMake(100, 100); } //定义每个UICollectionView 的 margin -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { //方位 return UIEdgeInsetsMake(5, 5, 5, 5); } //设置headView的回调函数 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { UICollectionReusableView *reusableview = nil; if (kind == UICollectionElementKindSectionHeader) { reusableview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath]; reusableview.backgroundColor = [UIColor redColor]; } UIView * view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,Screen_Width, 50)]; view.backgroundColor=[UIColor orangeColor]; [reusableview addSubview:view]; return reusableview; } #pragma mark --UICollectionViewDelegate //UICollectionView被选中时调用的方法 -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath]; //临时改变个颜色,看好,只是临时改变的。如果要永久改变,可以先改数据源,然后在cellForItemAtIndexPath中控制。(和UITableView差不多吧!O(∩_∩)O~) cell.backgroundColor = [UIColor greenColor]; NSLog(@"%d ",collectionView.tag); NSLog(@"item======%ld",(long)indexPath.item); NSLog(@"row=======%ld",(long)indexPath.row); NSLog(@"section===%ld",(long)indexPath.section); } //返回这个UICollectionView是否可以被选择 -(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath { return YES; } @end 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览