您的位置:首页 > 其它

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的单元格自动高度特性也不会生效.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: