iOS tableview中cell设置的注意事项

本文介绍如何在 iOS 应用中高级定制 TableView Cell 的样式,包括设置选中样式、背景视图、箭头指示符及图片显示等。此外还介绍了如何启用自动高度调整功能以简化布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

            /**

//             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:stylereuseIdentifier: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的单元格自动高度特性也不会生效.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值