您的位置:首页 > 移动开发 > IOS开发

单独使用collectionCell视图

2015-10-12 20:00 579 查看
偶然间在别人工程中看到的,方法很简单下面就让我来给大家说下 简单实现使用collectionCell 创建等同于view的视图

1.自定义collectionCell 添加你需要的属性

2.自定义一个view 声明一个方法 

#define SCREEN_WIDTH  [UIScreen mainScreen].bounds.size.width

#define floatByScreenWidth(a) ((a)/320.0)*SCREEN_WIDTH

- (void)setView;
实现:

- (void)setView//配置cell
{
    for (int i = 0;  i < 8; i++) {
        MyCollectionCell *cell = [[[NSBundle mainBundle]loadNibNamed:@"MyCollectionCell" owner:self options:nil]lastObject];
        cell.backgroundColor = [UIColor colorWithRed:arc4random()%255/255.0 green:arc4random()%255/255.0 blue:arc4random()%255/255.0 alpha:arc4random()%255/255.0];
        cell.nametitle.text = [NSString stringWithFormat:@"第%dcell",i];

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]init];
        [tap addTarget:self action:@selector(ClickVideoSites:)];
        cell.tag = i+1;
        tap.view.tag = i+1;
        [cell addGestureRecognizer:tap];

        
        int x = i/8;
        if (x<4) {
            cell.frame = [self setFrameBynum:i andPage:x];
        }else{
            cell.frame = [self setFrameBynum:i andPage:x];
        }
        [self addSubview:cell];
    }

}
//布局cell的位置

-(CGRect)setFrameBynum:(int)num andPage:(int)page
{
    CGFloat length = floatByScreenWidth(74);
    CGFloat height = length/74*85;
    CGFloat width = (SCREEN_WIDTH-4*length)/5;

    
    if (num  <  4) {
        return CGRectMake(width + page * SCREEN_WIDTH + (length+width)*num, 0, length, height);
    }
    else
        return CGRectMake(width + page * SCREEN_WIDTH + (length+width)*(num-4),height + 10, length, height);
}

3.Controller里面使用

    _myView = [[MyView alloc]initWithFrame:CGRectMake(0, 0, 320, 568)];
    [_myView setView];
    [self.view addSubview:_myView];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息