两种方式(xib和代码)创建自定义collectionview

相关代理方法参考:http://www.cnblogs.com/wayne23/p/4013522.html

http://www.cnblogs.com/ios8/p/iOS-UICollectionView.html


a.使用xib创建cell和ReusableView

1.添加对应的cell和ReusableView文件

2.在cell和ReusableView的xib文件中设置indentifier。

3.在viewDidLoad注册界面:

 [self.collectionViewregisterNib:[UINibnibWithNibName:@"testCell"bundle:nil]forCellWithReuseIdentifier:@“cell的identifier"];

 [self.collectionViewregisterNib:[UINibnibWithNibName:@"testHeadView"bundle:nil]forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"ReusableView的identifier"];

4.生成cell的代理方法中实现自己想要的效果:

    UICollectionViewCell *cell= [collectionViewdequeueReusableCellWithReuseIdentifier:cell的identifierforIndexPath:indexPath];

   testCell *test=(testCell *)cell;

    test.text=@"1!!!";

    return cell;

5.在生成ReusableView的代理方法中实现自己想要的效果:

//生成头

 if ([kindisEqualToString:UICollectionElementKindSectionHeader]) {

        reuseIdentifier=@“ReusableView的identifier”;

        UICollectionReusableView *view=[collectionViewdequeueReusableSupplementaryViewOfKind:kindwithReuseIdentifier:reuseIdentifierforIndexPath:indexPath];

        testHeadView *testView=(testHeadView *)view;

        testView.headLabel.text=@"这是文本1";

        testView.headLabel2.text=@"这是文本2";

        return view;

    }

    returnnil;


6.完成其他的基本代理


-------------------------------------------------------------------------------------------------------------------------------------



b.使用代码创建:

1.添加对应的cell和ReusableView文件(不生成xib)

2.在生成的.m文件中实现- (id)initWithFrame:(CGRect)frame方法:

//生成cell的自定义控件,ReusableView也是一样实现该方法,代码差不多

- (id)initWithFrame:(CGRect)frame

{

    self = [superinitWithFrame:frame];

    if (self) {

        // Initialization code

        self.backgroundColor = [UIColorpurpleColor];

        

        self.imgView = [[UIImageViewalloc]initWithFrame:CGRectMake(5,5,CGRectGetWidth(self.frame)-10,CGRectGetWidth(self.frame)-10)];

        self.imgView.backgroundColor = [UIColorgroupTableViewBackgroundColor];

        [selfaddSubview:self.imgView];

        

        self.text = [[UILabelalloc]initWithFrame:CGRectMake(5,CGRectGetMaxY(self.imgView.frame),CGRectGetWidth(self.frame)-10,20)];

        self.text.backgroundColor = [UIColorbrownColor];

        self.text.textAlignment =NSTextAlignmentCenter;

        [selfaddSubview:self.text];

        

        self.btn = [UIButtonbuttonWithType:UIButtonTypeCustom];

        self.btn.frame =CGRectMake(5,CGRectGetMaxY(self.text.frame),CGRectGetWidth(self.frame)-10,30);

        [self.btnsetTitle:@"按钮"forState:UIControlStateNormal];

        self.btn.backgroundColor = [UIColororangeColor];

        [selfaddSubview:self.btn];

    }

    returnself;

}

3.在viewDidLoad注册自定义的cell:

    //注册cellReusableView(相当于头部),ps:注册代码跟有xib的不一样

    [self.collectionViewregisterClass:[myCellclass]forCellWithReuseIdentifier:@"cell"];

    [self.collectionViewregisterClass:[UICollectionReusableViewclass]forSupplementaryViewOfKind:UICollectionElementKindSectionHeaderwithReuseIdentifier:@"ReusableView"];

4.在对应的生成方法中填充,生成/填充自定义cell的代码:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

    staticNSString *identify =@"cell";

    myCell *cell = [collectionViewdequeueReusableCellWithReuseIdentifier:identifyforIndexPath:indexPath];

    [cell sizeToFit];

    cell.text.text = [NSStringstringWithFormat:@"Cell %ld",indexPath.row];

    return cell;

}

5.完成其他的基本代理
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值