zoukankan      html  css  js  c++  java
  • 使用纯代码定义UICollectionView和自定义UICollectionViewCell

    1.自定义UICollectionView

    2.实现<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UICollectionViewDelegate>协议

     UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
                flowLayout.itemSize=CGSizeMake(60,60);
                [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
                
                UICollectionView *collectionView =[[UICollectionView alloc]initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:flowLayout];
                
                [collectionView registerClass:[customCollectCell class]
                        forCellWithReuseIdentifier:@"myIdentifier"];
                [collectionView setBackgroundColor:[UIColor whiteColor]];
                collectionView.dataSource=self;
                collectionView.delegate=self;
                
                [myControl addSubview:collectionView];
    

    3.自定义cell

      

    -(id)initWithFrame:(CGRect)frame{
        self=[super initWithFrame:frame];
        if (self) {
            UIView *view=[[UIView alloc]init];
            [view setFrame:CGRectMake(5, 0, 60, 60)];
            view.layer.cornerRadius=4;
            view.layer.masksToBounds=YES;
         //item被选中时的背景色,可以不用设置 [view setBackgroundColor:[UIColor lightGrayColor]]; self.selectedBackgroundView=view; float offset_x=5; float offset_y=0; { _myimageView=[[UIImageView alloc]initWithFrame:CGRectMake(15, offset_y, 60,60)]; [_myimageView setContentMode:UIViewContentModeScaleAspectFill]; [self addSubview:_myimageView]; _myLabel=[[UILabel alloc]initWithFrame:CGRectMake(offset_x, offset_y+70, 60, 20)]; [_myLabel setTextAlignment:NSTextAlignmentCenter]; [_myLabel setFont:[UIFont systemFontOfSize:12]]; [_myLabel setTextColor:[UIColor redColor]]; [self addSubview:_myLabel]; } } return self; }

     

  • 相关阅读:
    新的for增强循环方法,记录一下,方便以后使用
    Intellij IDEA 自动生成 serialVersionUID
    Java知识点汇总[Review]
    D16-常用十种算法[Java数据结构和算法]
    W9-请求响应[JavaWeb]
    D15-图[Java数据结构和算法]
    D14-多路查找树[Java数据结构和算法]
    D13-平衡二叉树[Java数据结构和算法]
    D12-二叉排序树[Java数据结构和算法]
    D11-堆排序和赫夫曼编码[Java数据结构和算法]
  • 原文地址:https://www.cnblogs.com/niit-soft-518/p/4225584.html
Copyright © 2011-2022 走看看