防止cell里面的子控件重叠方法
来源:互联网 发布:2017淘宝有多少家店铺 编辑:程序博客网 时间:2024/05/21 10:24
有时候。我们没有自定义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
- 防止cell里面的子控件重叠方法
- iOS 自定义cell点击后里面的子控件背景颜色被改变解决办法
- css防止边距重叠的方法
- cell里面控件宽度布局的问题
- cell调用其他控件,造成重叠的解决
- UICollectionView使用reloadData()方法之后,Cell 里面的控件报错找不到
- UICollectionView使用reloadData()方法之后,Cell 里面的控件报错找不到
- UITableView 的cell重叠问题
- ios UICollectionView的cell重叠
- 如何遍历对话框里面的子控件
- swift 里面tableview的cell代码创建方法
- 在自定义cell里面实现跳转的方法
- tableView的复用问题----里面的子视图内容重叠!
- cell 控制子控件的显示和隐藏
- CSS外边距重叠及防止方法
- cell的文字会重叠在一起
- 控件重叠透明问题的简单处理方法
- ASP.Net访问母版页(MasterPage)控件、属性、方法及母版页中调用内容页,获取用户自定义控件里面的子控件的方法
- linux mysql 中大小写敏感问题
- 安卓socket聊天,端口被占用问题(附:socket聊天代码)(一)
- Ubuntu下安装mysql过程记录
- html5绘图
- 黑马程序员_java基础_String类和基本数据类型的包装类
- 防止cell里面的子控件重叠方法
- OC中字符串的排序
- C编译错误--multiple definition of 'XXX'
- MySQL-数据库总结
- cmd命令记录
- 浅谈框架以及类库
- HashMap和HashSet的区别
- 提高数据易用性(10)-“数据条”充当柱状图
- 双语:今年最热的网络小说改编剧,就是这六部