ios 集合视图(九宫格布局)

利用UIViewController,UIView,UICollectionViewCell,通过重写UICollectionViewDelegate,UICollectionViewDelegateFlowLayout, 协议的一些方法实现(以及UICollectionViewDataSource)

1,在RootViewController里面

    //设置代理
    self.rtView.coll.dataSource = self;
    self.rtView.coll.delegate = self;
    //注册cell
    [self.rtView.coll registerClass:[myCell class] forCellWithReuseIdentifier:kCollCell];
    //在设置header时要用到
    //注册header
    [self.rtView.coll registerClass:[UICollectionReusableView class] 
       forSupplementaryViewOfKind:UICollectionElementKindSectionHeader 
       withReuseIdentifier:kHeaderView];
    //注册footer
    [self.rtView.coll registerClass:[UICollectionReusableView class] 
       forSupplementaryViewOfKind:UICollectionElementKindSectionFooter 
       withReuseIdentifier:kFooterView];
//返回cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView 
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    myCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollCell 
        forIndexPath:indexPath];
    cell.backgroundColor = [UIColor colorWithRed:kColorChange green:kColorChange 
        blue:kColorChange alpha:1];
    return cell;
}
//设置header和footer
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView 
     viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)
     indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:
             UICollectionElementKindSectionHeader withReuseIdentifier:kHeaderView 
             forIndexPath:indexPath];
        header.backgroundColor = [UIColor yellowColor];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 
             header.bounds.size.height)];
        label.text = @"哈哈";
        [header addSubview:label];
        return header;
    }else{
        UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:
            UICollectionElementKindSectionFooter withReuseIdentifier:kFooterView 
            forIndexPath:indexPath];
        return footer;
    }
    
}
2,在RootView里面

 

//视图布局
-(void)addAllViews
{
    //1,创建UICollectionViewFlowLayout
    //对cell进行布局控制
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    //设置
    //1.1,设置大小
    flowLayout.itemSize = CGSizeMake(80, 100);
    //1.2,设置间距(如果给定的间距,无法满足屏幕宽度,设置无效)
    flowLayout.minimumInteritemSpacing = 10;
    //1.3,设置行间距
    flowLayout.minimumLineSpacing = 10;
    //1.4,设置滑动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //1.5,heater
    flowLayout.headerReferenceSize = CGSizeMake(self.bounds.size.width, 30);
    //1.6,footer
    flowLayout.footerReferenceSize = CGSizeMake(self.bounds.size.width, 100);
    //1.7,设置字内边框
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //2,创建集合视图
    self.coll = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen 
     mainScreen].bounds.size.width, CGRectGetHeight(self.frame)) collectionViewLayout:
     flowLayout];
    self.coll.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.coll];
}
3,MyCell里面(继承于UICollectionViewCell)
//视图布局
-(void)addAllViews
{
    //视图空间加载在contentView上
    self.imV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 
        self.contentView.bounds.size.width, self.contentView.bounds.size.height-20)];
    self.imV.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:self.imV];
    
    self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.imV.frame), 
        self.contentView.bounds.size.width, self.contentView.bounds.size.height - 
        CGRectGetHeight(self.imV.frame))];
    self.myLabel.text = @"nice";
    self.myLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.myLabel];
}
//一旦改变,重新显示
-(void)layoutSubviews
{
    [super layoutSubviews];
    self.imV.frame = CGRectMake(0, 0, self.contentView.bounds.size.width, 
          self.contentView.bounds.size.height-20);
    self.myLabel.frame = CGRectMake(0, CGRectGetMaxY(self.imV.frame), 
          self.contentView.bounds.size.width, self.contentView.bounds.size.height - 
          CGRectGetHeight(self.imV.frame));
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值