tableview中cell设置的注意事项
2016-05-25 02:02
239 查看
/**
// selectionStyle :设置选中样式
// backgroundView:设置cell背景
//
// 1.
当两个属性同时实现, 则backgroundView会起作用
// 2.
从iOS7以后,就是灰色,没有蓝色
// 3.
如果selectionStyle设置为None,则backgroundView失效
// */
//
// // 设置选中样式 -->蓝色iOS6默认颜色 -->从iOS7以后,就是灰色
// //cell.selectionStyle = UITableViewCellSelectionStyleBlue;
//
// // 设置左边的普通状态背景
// cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_dropdown_leftpart"]];
//
// // 设置左边的选中状态背景
// cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_dropdown_left_selected"]];
// }
//
// //1. 获取模型
// HMCategoryModel *categoryModel = self.categoryArray[indexPath.row];
//
// cell.textLabel.text = categoryModel.name;
//
// //2. 设置右边箭头 -->根据,有没有子分类数据来判断
// if (categoryModel.subcategories.count) {
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// } else {
// cell.accessoryType = UITableViewCellAccessoryNone;
// }
//
// //3. 设置图标
// cell.imageView.image = [UIImage imageNamed:categoryModel.icon];
//
// //4. 设置选中高亮图标
// cell.imageView.highlightedImage = [UIImage imageNamed:categoryModel.highlighted_icon];
//
++++++++在自定义的cell中要实现这个方法:
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString
*)reuseIdentifier{
if(self=[superinitWithStyle:style
reuseIdentifier:reuseIdentifier]){
[selfaddlblView];
}
returnself;
}
===========在普通的view中要实现这个方法
//-(instancetype)initWithFrame:(CGRect)frame{
// if(self ==[super initWithFrame:frame]){
//
// [self addlblView];
// }
// return self;
//}
==============estimatedRowHeight
其实核心代码就有两句话,上代码
self.tableView.estimatedRowHeight = 44 ;
//44为任意值
self.tableView.rowHeight = UITableViewAutomaticDimension;
记得第一次使用这个"黑科技"还是在一年多以前,后来几乎被我遗忘了,一年多以前正是iOS8的尾巴时期,属于一个新特性,因为这个只能适配iOS8+,如今已经是iOS10
的阶段,我个人感觉使用起来完全没有兼容性问题,如果你的公司考虑的太多,慎用啊!!!
前提
这个"黑科技"的前提是,cell中的子控件的布局要使用autolayout,因为tableView的特性,子控件是由内向外布局的,所以预设高度是必须的.第二局句的作用如前面所述,是开启iOS
8的单元格的自适应高度特性。第一句代码也是同样的功能,estimatedRowHeight让你提供一个预先估计cell的高度值,这个值根本可以乱设(只要不为0),但如果你不写这句,或者将estimatedRowHeight属性设置为0,则iOS
8的单元格自动高度特性也不会生效.
// selectionStyle :设置选中样式
// backgroundView:设置cell背景
//
// 1.
当两个属性同时实现, 则backgroundView会起作用
// 2.
从iOS7以后,就是灰色,没有蓝色
// 3.
如果selectionStyle设置为None,则backgroundView失效
// */
//
// // 设置选中样式 -->蓝色iOS6默认颜色 -->从iOS7以后,就是灰色
// //cell.selectionStyle = UITableViewCellSelectionStyleBlue;
//
// // 设置左边的普通状态背景
// cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_dropdown_leftpart"]];
//
// // 设置左边的选中状态背景
// cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_dropdown_left_selected"]];
// }
//
// //1. 获取模型
// HMCategoryModel *categoryModel = self.categoryArray[indexPath.row];
//
// cell.textLabel.text = categoryModel.name;
//
// //2. 设置右边箭头 -->根据,有没有子分类数据来判断
// if (categoryModel.subcategories.count) {
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// } else {
// cell.accessoryType = UITableViewCellAccessoryNone;
// }
//
// //3. 设置图标
// cell.imageView.image = [UIImage imageNamed:categoryModel.icon];
//
// //4. 设置选中高亮图标
// cell.imageView.highlightedImage = [UIImage imageNamed:categoryModel.highlighted_icon];
//
++++++++在自定义的cell中要实现这个方法:
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString
*)reuseIdentifier{
if(self=[superinitWithStyle:style
reuseIdentifier:reuseIdentifier]){
[selfaddlblView];
}
returnself;
}
===========在普通的view中要实现这个方法
//-(instancetype)initWithFrame:(CGRect)frame{
// if(self ==[super initWithFrame:frame]){
//
// [self addlblView];
// }
// return self;
//}
==============estimatedRowHeight
其实核心代码就有两句话,上代码
self.tableView.estimatedRowHeight = 44 ;
//44为任意值
self.tableView.rowHeight = UITableViewAutomaticDimension;
记得第一次使用这个"黑科技"还是在一年多以前,后来几乎被我遗忘了,一年多以前正是iOS8的尾巴时期,属于一个新特性,因为这个只能适配iOS8+,如今已经是iOS10
的阶段,我个人感觉使用起来完全没有兼容性问题,如果你的公司考虑的太多,慎用啊!!!
前提
这个"黑科技"的前提是,cell中的子控件的布局要使用autolayout,因为tableView的特性,子控件是由内向外布局的,所以预设高度是必须的.第二局句的作用如前面所述,是开启iOS
8的单元格的自适应高度特性。第一句代码也是同样的功能,estimatedRowHeight让你提供一个预先估计cell的高度值,这个值根本可以乱设(只要不为0),但如果你不写这句,或者将estimatedRowHeight属性设置为0,则iOS
8的单元格自动高度特性也不会生效.
相关文章推荐
- 用python记录运行pid,并在需要时kill掉它们
- cs231n 卷积神经网络与计算机视觉 2 SVM softmax
- [Ruby笔记]13.Ruby object .replace("") .dup .freeze
- Android开发:shape和selector和layer-list的(详细说明)
- iOS之CoreImage图像处理框架
- NYOJ-27-水池数目
- cocos2d-x 两个场景间进行传参数
- 完全删除TFS2013上的项目
- cocos2d-x 设置全局可变变量
- java中的命名规范
- 操作系统-操作系统引论
- eclipse开发时常用的快捷键
- LeetCode 160. Intersection of Two Linked Lists
- odoo 8 qweb Report英文版
- 如何将cocos2d-x项目打包成一个.exe
- LeetCode 165. Compare Version Numbers(比较版本)
- LeetCode 164. Maximum Gap(最大空隙)
- LeetCode 163. Missing Ranges(缺失区间)
- LeetCode 162. Find Peak Element(查找峰值)
- LeetCode 161. One Edit Distance(编辑距离)