防止cell里面的子控件重叠方法

有时候。我们没有自定义cell的时候,我们需要给uitableview自带的cell里面添加一些控件。比如我往cell里面再加入一个uilabel和一个iconview。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
      static NSString *identifier=@"moreiden";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
      if (cell==nil) {
          cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault       reuseIdentifier:identifier];
          [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
      }
      //这里开始加入两个控件
        UIImageView *iconview=[[UIImageView alloc]init];
        //设置圆角
        iconview.layer.masksToBounds=YES;
        iconview.layer.cornerRadius=30/2;
        iconview.frame=CGRectMake(50, 15, 30, 30);//随便定的位置
        [iconview setImage:[UIImage imageNamed:@"tupian.png"]];
        [cell.contentView addSubview:iconview];

        UILabel *textlab=[[UILabel alloc]init];
        textlab.text=@"我是新加进来的label";
        textlab.font=[UIFont systemFontOfSize:12];
        textlab.textColor=[UIColor blackColor];
        textlab.frame=CGRectMake(100, 10, 80, 30);//随便定的位置
        [cell.contentView addSubview:textlab];
        return cell;
}

这样,每次cell里面不仅仅有自己的一些子控件,还有一些我们加入的子控件,可是问题来了。如果这个页面没有在切换时销毁,那么每次我们切换页面的时候,cell里面就会每次都加两个子控件,而且位置都一样,这样来回切换几次,会发现我们的cell里面加入的那些子控件已经重叠变形了。如果我们打开断点会发现,每次切换时,cell.contentView.subviews.count就会增加两个。怎么办呢。这时候,我们就需要在每次往cell里面加东西时,做一次遍历,把之前加入的控件都删掉就行了。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
      static NSString *identifier=@"moreiden";
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
      if (cell==nil) {
          cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault       reuseIdentifier:identifier];
          [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
      }
         //防止每次切换页面都往cell添加子空间,造成重叠
        for (id subview in cell.contentView.subviews) {
            [subview removeFromSuperview];
        }
      //这里开始加入两个控件
        UIImageView *iconview=[[UIImageView alloc]init];
        //设置圆角
        iconview.layer.masksToBounds=YES;
        iconview.layer.cornerRadius=30/2;
        iconview.frame=CGRectMake(50, 15, 30, 30);//随便定的位置
        [iconview setImage:[UIImage imageNamed:@"tupian.png"]];
        [cell.contentView addSubview:iconview];

        UILabel *textlab=[[UILabel alloc]init];
        textlab.text=@"我是新加进来的label";
        textlab.font=[UIFont systemFontOfSize:12];
        textlab.textColor=[UIColor blackColor];
        textlab.frame=CGRectMake(100, 10, 80, 30);//随便定的位置
        [cell.contentView addSubview:textlab];
        return cell;
}

ok ,大功告成!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值