tableview上cell 加载collectionview

在开发中,或许美工给你的UI设计图 上有很复杂的界面设计,比如一个视图上明显是tableview  但是上面也有collectionview  这时候怎么办,不要慌,下面我教你怎么写。

1首先,创建整体UI界面

- (void)buildUI{

    

    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, VIEW_Width, VIEW_Height)];

    _tableview.delegate = self;

    _tableview.dataSource = self;

    _tableview.rowHeight = 200;

    _tableview.backgroundColor = [UIColor greenColor];

    [self.view addSubview:_tableview];

    

    

    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 200, VIEW_Width, 200) collectionViewLayout:[self customLayout]];

    _collectionView.backgroundColor = [UIColor redColor];

    _collectionView.delegate = self;

    _collectionView.dataSource = self;

    

    [_collectionView registerNib:[UINib nibWithNibName:@"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];

    

    [_tableview addSubview:_collectionView];


    

}


2,剩下的就都是实现代理了

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


    return 3;

}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


    if (indexPath.section == 0) {

        NSLog(@"000000");

    }else if (indexPath.section == 1){

    

        NSLog(@"index11111");

    }else{

    

        NSLog(@"index22");

    }

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    

    static NSString *idenfier = @"cellID";


    

    if (indexPath.section == 1) {

        

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idenfier];

        if (!cell) {

            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:idenfier];

            

        }

        cell.imageView.image = [UIImage imageNamed:@"af4"];


        return cell;


    }else{


        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:idenfier];

        if (!cell) {

            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:idenfier];

            

        }

        cell.imageView.image = [UIImage imageNamed:@"af3"];

        return cell;

    }

    

}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{


    return 4;

    


}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 10;

}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:

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


    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    return cell;

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    

    if (indexPath.item == 0) {

        NSLog(@"0");

    }else if(indexPath.item == 1){

        

        NSLog(@"1");

    }else{

        

        NSLog(@"2");

    }

    

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

    return @"hah";

}

- (CGSize )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath

{

    return  CGSizeMake(VIEW_Width / 3,120);

}

这样 一个在tableview上cell自定义的collection view就写好了 其实很简单
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值