使用纯代码定义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;
}

 

时间: 2024-08-04 15:12:47

使用纯代码定义UICollectionView和自定义UICollectionViewCell的相关文章

iOS开发 纯代码创建UICollectionView

转:http://jingyan.baidu.com/article/eb9f7b6d8a81a5869364e8a6.html iOS开发 纯代码创建UICollectionView 习惯了使用xib和StoryBoard创建UICollectionView项目工程的伙伴,需要转换使用纯代码来实现,想避免碰更多的壁,就需要认真了解创建UICollectionView过程了.创建UICollectionView比创建UITableView更加复杂,初始化方式也是相对奇特.以下是使用纯代码创建UI

通过Xib创建 UICollectionView 和自定义UICollectionViewCell

1.在控制器的viewDidLoad方法中添加代码 CGFloat itemWidth = (kScreenW - kSpacingW * 3) / 2; NSLog(@"itemWidth == %f",itemWidth); CGFloat itemHeight = itemWidth * 0.75 + 71; NSLog(@"itemWidth == %f",itemHeight); UICollectionViewFlowLayout*layout = [[

iOS开发UI篇—以微博界面为例使用纯代码自定义cell程序编码全过程(一)

iOS开发UI篇-以微博界面为例使用纯代码自定义cell程序编码全过程(一) 一.storyboard的处理 直接让控制器继承uitableview controller,然后在storyboard中把继承自uiviewcontroller的控制器干掉,重新拖一个tableview controller,和主控制器进行连线. 项目结构和plist文件 二.程序逻辑业务的处理 第一步,把配图和plist中拿到项目中,加载plist数据(非png的图片放到spooding files中) 第二步,字

IOS开发UI篇--UITableView的自定义布局==纯代码布局

UITableView中除了利用系统的UItableViewCell不能完成需求进行布局时,还可以进行自定义布局: 自定义布局分为两类:(1)利用代码进行创建 (2)利用xib进行实现: 下面对利用代码进行创建分析: 应用场景:像微博,等列表数据展示(由于微博的每个单元格的数据大小不一致,所以得计算每个单元格的大小) 分析:前提是获取列表数据,然后建立每个单元格的模型(建立单元格模型应继承UITableViewCell)复写 - (id)initWithStyle:(UITableViewCel

纯代码实现自定义UITableView的cell

纯代码实现自定义UITableView的cell 新建一个继承自UITableViewCell的类 重写initWithStyle:reuseIdentifier:方法,在里面实现: 添加所有需要显示的子控件(不需要设置子控件的数据和frame,子控件要添加到contentView中) 进行子控件一次性的属性设置(有些属性只需要设置一次, 比如字体\固定的图片) BNPSettingCell.h文件: /*本代码实现自定义cell的分隔线*/ #import <UIKit/UIKit.h> @

猫猫学IOS(十七)UI之纯代码自定义Cell实现新浪微博UI

猫猫分享,必须精品 素材代码地址:http://blog.csdn.net/u013357243/article/details/44976175 原文地址:http://blog.csdn.net/u013357243?viewmode=contents 先看效果图 编程思路 代码创建Cell的步骤 1> 创建自定义Cell,继承自UITableViewCell 2> 根据需求,确定控件,并定义属性 3> 用getter方法完成控件的实例化,只创建并添加到contentView,不处理

(素材源码)猫猫学IOS(十七)UI之纯代码自定义Cell实现新浪微博UI

猫猫分享,必须精品 素材代码地址:http://download.csdn.net/detail/u013357243/8580249 原文地址:http://blog.csdn.net/u013357243?viewmode=contents 先看效果图 编程思路 代码创建Cell的步骤 1> 创建自定义Cell,继承自UITableViewCell 2> 根据需求,确定控件,并定义属性 3> 用getter方法完成控件的实例化,只创建并添加到contentView,不处理位置 4&g

ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局

本文转自 :http://www.cnblogs.com/wendingding/p/3761730.html ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局 一.实现效果 二.使用纯代码自定义一个tableview的步骤 1.新建一个继承自UITableViewCell的类 2.重写initWithStyle:reuseIdentifier:方法 添加所有需要显示的子控件(不需要设置子控件的数据和frame,  子控件要添加到contentView中

iOS开发--xib自定义UIView,与IB 、 xib 、 代码 定义与初始化对比

一.自定义UIView 二.关于xib定义的view实例化,初始化. 三.使用IB VS xib  VS 使用代码定义与实例化