自定义UITableViewCell需注意的问题
自定义cell,有下面几种方法
方法一:
在controller的.m中
1 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> 2 3 @property(nonatomic,strong) UITableView *tableView; 4 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 12 self.tableView.delegate = self; 13 self.tableView.dataSource = self; 14 15 ///若cell内无数据,就去除多余cell 16 self.tableView.tableFooterView = [[UIView alloc]init]; 17 18 [self.view addSubview:self.tableView]; 19 } 20 21 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 22 return 10; 23 } 24 25 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 26 static NSString *cellIdentifier = @"myCell"; 27 UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 28 if (myCell == nil) { 29 myCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellIdentifier]; 30 } 31 32 UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 20)]; 33 label.text = @"这个是自定义的cell"; 34 [myCell addSubview:label]; 35 36 return myCell; 37 } 38 39 -(UITableView *)tableView{ 40 if (!_tableView) { 41 _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ViewWidth, ViewHeight)]; 42 } 43 return _tableView; 44 } 45 46 -(BOOL)prefersStatusBarHidden{ 47 return YES; 48 } 49 50 @end
这样可以实现效果,但若上下滑动时,会有几行cell字体加粗。如图:
原因在于cell复用时,由于每个服用的cell都会添加label,所以造成label的重叠。
解决问题的方法就是自定义UITableViewCell
方法二:
如想在cell里添加两个button,则需要自定义一个类,如在.h文件中,button在.h文件中定义
1 @interface ActivieyViewCell : UITableViewCell 2 /** 活动内容btn */ 3 @property(nonatomic,strong) UIButton *contentBtn; 4 /** 活动花费btn */ 5 @property(nonatomic,strong) UIButton *costBtn; 6 7 /** 设置button的位置等信息 */ 8 - (void)setupCellBtn; 9 @end
在.m文件中,直接在自定义方法里定义cell要添加的内容
1 @implementation ActivieyViewCell 2 -(void)setupCellBtn{ 3 self.contentBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 4 self.contentBtn.frame = CGRectMake(10, 10, 400, 20); 5 self.contentBtn.backgroundColor = [UIColor brownColor]; 6 [self addSubview:self.contentBtn]; 7 8 self.costBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 9 self.costBtn.frame = CGRectMake(500, 10, 40, 20); 10 self.costBtn.backgroundColor = [UIColor lightGrayColor]; 11 [self addSubview:self.costBtn]; 12 } 13 @end
在tableView的cellForRowAtIndexPath中添加
1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 2 static NSString *cellIdentify = @"activityList"; 3 //在该处就可以使用自定义的tableViewCell 4 //前边的都是用 5 //UITableViewCell *actTabCell = [tableView dequeueReusableCellWithIdentifier:cellIdentify]; 6 ActivieyViewCell *actTabCell = [tableView dequeueReusableCellWithIdentifier:cellIdentify]; 7 8 if (actTabCell==nil) { 9 actTabCell = [[ActivieyViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify]; 10 } 11 12 //在该处定义button的位置信息等 13 [actTabCell setupCellBtn]; 14 15 [actTabCell.contentBtn setTitle:actModel.activityContent forState:UIControlStateNormal]; 16 [actTabCell.contentBtn setBackgroundColor:[UIColor brownColor]]; 17 actTabCell.contentBtn.titleLabel.numberOfLines = 0; 18 19 [actTabCell.costBtn setTitle:actModel.activityCost forState:UIControlStateNormal]; 20 [actTabCell.costBtn setBackgroundColor:[UIColor lightGrayColor]]; 21 actTabCell.costBtn.titleLabel.numberOfLines = 0; 22 23 return actTabCell; 24 }
这样写的好处就是tableViewCell在复用时不会出现位置混乱等bug.
但上述自定义cell的方法还有一个bug,还是cell的复用问题。点击cell时,背景有时会变乱.如图
原因还是上边的cell复用问题。改进方法是将setupCellBtn方法写到initWithStyle中,在初始化时就设置。然后在cellForRowAtIndexPath无需使用[actTabCell setupCellBtn];这样的方法
方法三:
在UITableViewCell的.m中
1 @implementation ActivieyViewCell 2 //重写该方法,防止复用时混乱 3 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{ 4 self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 5 if (self) { 6 [self setupBtnFrame]; 7 } 8 return self; 9 } 10 11 -(void)setupCellBtn{ 12 self.contentBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 13 self.contentBtn.frame = CGRectMake(10, 10, 400, 20); 14 self.contentBtn.backgroundColor = [UIColor brownColor]; 15 [self addSubview:self.contentBtn]; 16 17 self.costBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 18 self.costBtn.frame = CGRectMake(500, 10, 40, 20); 19 self.costBtn.backgroundColor = [UIColor lightGrayColor]; 20 [self addSubview:self.costBtn]; 21 } 22 @end
然后在tableView的cellForRowAtIndexPath中添加
1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 2 static NSString *cellIdentify = @"activityList"; 3 //在该处就可以使用自定义的tableViewCell 4 //前边的都是用 5 //UITableViewCell *actTabCell = [tableView dequeueReusableCellWithIdentifier:cellIdentify]; 6 ActivieyViewCell *actTabCell = [tableView dequeueReusableCellWithIdentifier:cellIdentify]; 7 8 if (actTabCell==nil) { 9 actTabCell = [[ActivieyViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify]; 10 } 11 12 //原来是在该处定义button的位置信息等,现在删除 13 //[actTabCell setupCellBtn]; 14 15 [actTabCell.contentBtn setTitle:actModel.activityContent forState:UIControlStateNormal]; 16 [actTabCell.contentBtn setBackgroundColor:[UIColor brownColor]]; 17 actTabCell.contentBtn.titleLabel.numberOfLines = 0; 18 19 [actTabCell.costBtn setTitle:actModel.activityCost forState:UIControlStateNormal]; 20 [actTabCell.costBtn setBackgroundColor:[UIColor lightGrayColor]]; 21 actTabCell.costBtn.titleLabel.numberOfLines = 0; 22 23 return actTabCell; 24 }
这样就再也不会出现重叠问题,所以推荐使用方法三,来自定义tableViewCell
转载于:https://www.cnblogs.com/Apologize/p/4683558.html
自定义UITableViewCell需注意的问题相关推荐
- 对自定义UITableViewCell的理解
自定义UITableViewCell有两种方法: 1.较早版本 子类UITableViewCell 并利用xib构造 2.利用storyboard直接自定义cell 1.利用xib 设计好自定义的 ...
- iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
iOS开发UI篇-使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 iOS开发UI篇-使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目 ...
- 自定义UITableViewCell新浪微博
自定义UITableViewCell新浪微博 自定义 UITableViewCell的步骤: 1. 新建一个继承自 UITableViewCell 的类 2. 重写 initWithStyle:reu ...
- UI一揽子计划 11 (自定义UITableViewCell、Cell 的自适应高度)
一. 自定义UITableViewCell 在日常的编程中,系统提供的几种Cell 样式 往往不能满足我们的需求.所以需要我们给它进行自定义样式. 自定义Cell 就是创建一个UITableViewC ...
- 【swift学习笔记】三.使用xib自定义UITableViewCell
使用xib自定义tableviewCell看一下效果图 1.自定义列 新建一个xib文件 carTblCell,拖放一个UITableViewCell,再拖放一个图片和一个文本框到tableviewc ...
- UITableView 系列五 :自定义UITableViewCell (实例)
有时候我们需要自己定义UITableViewCell的风格,其实就是向行中添加子视图.添加子视图的方法主要有两种:使用代码以及从.xib文件加载.当然后一种方法比较直观. 我们这次要自定义一个Cell ...
- 解决自定义UITableViewCell在浏览中出现数据行重复的问题
我在写一个App的时候自定义了一个UITableViewCell,但是这个UITableView在运行的时候出现了每6行数据就循环重复显示的问题,而直接使用cell.textLabel.text显示是 ...
- 自定义UITableViewCell实现ibooks类似的图书列表形式
前几天实现iBooks类似的图书列表形式,share一下,效果如下. 实现关键代码原理: 1:创建UIt=TableView对象时,设置背景透明,分隔条透明 [plain] view plaincop ...
- Swift 使用xib自定义UITableViewCell
使用xib自定义tableviewCell看一下效果图 1.自定义列 新建一个xib文件 carTblCell,拖放一个UITableViewCell,再拖放一个图片和一个文本框到tableviewc ...
最新文章
- sublime_text配置php调试环境,SublimeText2配置PHP调试环境(在windows环境下)
- lamp 环境搭建论坛
- C语言实现图形ADT(Graph ADT)接口COMP2521(附完整源码)
- jvm指令重排原因?怎么避免?
- Spring@Schedule定时任务源码解析
- Word文档加密技巧
- python案例数据集_Python数据集切分实例
- ubuntu如何删除刚添加的源?
- spring配置定时器的时间设置
- Shell in AIX Web端 自动远程执行重启tomcat服务命令
- 文件与i o流java实_JavaFile I/O流
- 电阻电容等封装对应功率
- 微信H5(WAP)支付,遇到的那些坑
- 大话设计模式之爱你一万年:第一章 设计模式基本概念:1.软件设计模式概述
- 统计学③——总体与样本的差异在哪里
- 瀚高数据库并行导入导出
- 2017年网易推出他们的“淘宝客”啦
- 雅利安人有多强悍?灭掉三个文明古国,为何败在殷商的脚下
- UG NX 三维PMI标注培训
- 机器学习——朴素贝叶斯分类
热门文章
- Linux vim剪切/删除命令(结合p粘贴使用就是剪切)
- python url编码 空格_使用请求和python时URL中的空格
- python变量名包括_Python – 获取全局范围内对象的所有变量名
- ELF格式文件特点的简单说明
- Elasticsearch、Elasticsearch-head、Kibana 安全设置账号密码验证登陆
- 无效0_12位浙江高考生成绩被教育考试院判定无效,0分收场的原因很可惜
- 启明云端感谢热心开发者分享SSD20x直接编译lvgl7.10带双缓(附源码)
- 发福利了|最近发现深圳有一家公司提供的叫8ms的GUI平台不错,好用,最主要所有功能都是免费的
- java 中调用docker_如何通过Java程序执行docker命令
- 百度AI原生云实践: 基于容器云打造 AI 开发基础设施