博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS学习31之UITableVIewCell自定义
阅读量:7066 次
发布时间:2019-06-28

本文共 5879 字,大约阅读时间需要 19 分钟。

1. 自定义Cell

 1> 为什么要自定义Cell

  • UITableView 中系统的Cell共提供了四种默认样式,  分别是:

    UITableViewCellStyleDefault

    UITableViewCellStyleValue1

    UITableViewCellStyleValue2  

    UITableViewCellStyleSubtitle

  但是在实际使用过程中, Cell样式的布局上千差万别, 因此我们需要自定义Cell

  • 在前期我们学过自定义视图, 即创建一个类继承于 UIView 或者 其他的视图, 在自定义类中创建其子视图, 这样就会形成一个新的自定义视图。

  • 系统提供的cell满足不了复杂的样式,因此: 定义Cell和自定义视图一样,自己创建一种符合我们需求的Cell并使用这个Cell。

 2> 自定义Cell的步骤

  • 创建一个类继承于 UITableViewCell

  • 实现 UITableViewCell初始化方法:

1 // 初始化 2 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 3     self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 4     if (self) { 5         //初始化子视图 6         [self initLayout]; 7     } 8     return self; 9 }10 11 //初始化子视图布局方法12 - (void)initLayout {13     14     //1.头像15     self.headerImageView = [[UIImageView alloc]initWithFrame:CGRectMake(16, 10, 100, 100)];16     17     self.headerImageView.layer.masksToBounds = YES;18     19     self.headerImageView.layer.cornerRadius = CGRectGetWidth(self.headerImageView.frame) / 2;20     21     // cell提供一个contactVIew属性,专门用来自定义cell,防止在cell布局的时候发生布局混乱,如果是自定义cell,记得将子控件添加到contactVIew上22     [self.contentView addSubview:self.headerImageView];23     24     //2.姓名25     self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.headerImageView.frame) + 10, CGRectGetMinY(self.headerImageView.frame), 100, 40)];26     27     [self.contentView addSubview:self.nameLabel];28     29     //3.电话号码30     self.phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMinX(self.nameLabel.frame), CGRectGetMaxY(self.nameLabel.frame) + 20, 200, 40)];31 32     [self.contentView addSubview:self.phoneLabel];33 }

contentViewCell 提供一个属性专门用来自定义 Cell 防止Cell 布局的时候发生布局混乱,如果是自定义 Cell记得将子控件添加到 contactVIew 上

效果图:

  • 确保所有的你想添加的子视图都在自定义 Cell初始化方法创建, 由于 UITableView 的重用机制, 一个 Cell 在第一次创建成功并用于下一次显示的时候,不会再走初始化方法, 这样可以避免子视图的重复创建。
  • Cell 的子视图创建成功后,将视图设置为属性,类似于 UITableViewCell 所自带的 textLabeldetailTextLabel 属性。便于在 UITableView 的协议中给自定义视图赋值。

2. 多种Cell混合使用

 1> 概述

  在 TableView 中不仅仅只显示一种 Cell , 多种 Cell 可以并存

 2> 使用场景

  一个重用标识符只能针对于一种 Cell 样式, 不同的 Cell 需要基于不同的重用标识符来进行区分, 重用标识符的区分需要根据不同的情况来划分, 如:

  • model 属性划分(不同的数据内容,比如一个数据中有 type 字段, 为1代表图片类型, 为0代表文字类型)
1 Model *model = [self.tableArray objectAtIndex:indexPath.row]; 2 //根据model属性划分 3 if (model.type == 0) { 4     FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:firstIdentify]; 5     return cell; 6 } 7 if (model.type == 1) { 8     SecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:secondIdentify]; 9     return cell;10 }
  • 固定的行显示的 Cell 类型不一样
1 // 第一行显示第一种Cell 2 if (indexPath.row == 0) { 3     FirstTableViewCell *cell = [tableView 4                                 dequeueReusableCellWithIdentifier:firstIdentify]; 5     return cell; 6 } 7 // 第二行显示第二种Cell 8 if (indexPath.row == 1) { 9     SecondTableViewCell *cell = [tableView10                                  dequeueReusableCellWithIdentifier:secondIdentify];11     return cell;12 }

3. Cell自适应高度 

 1> 自适应高度的两种应用

  ① 文本自适应高度:根据文本内容设定Label高度

   第一步: 修改 Label 的高度(在 - (void)layoutSubView 方法中 )

1 - (void)layoutSubviews {2     3     // 1. 获取文本高度4     CGFloat textHeight = [Tool textHeightWithText:self.myLabel.text font:[UIFont systemFontOfSize:20]];5     // 2.修改label的frame6     self.myLabel.frame = CGRectMake(0, 0, self.bounds.size.width, textHeight);7 }    

   获取文本高度的方法(一般封装在工具类中)

1 // 计算文本高度 2 + (CGFloat)textHeightWithText:(NSString *)text font:(UIFont *)font { 3      4     // iOS7.0中求文本高度的方法,返回一个CGRect的高度 5      6     // 第一个参数 7     CGSize size = CGSizeMake([UIScreen mainScreen].bounds.size.width, 10000); 8      9     // 第二个参数:设置以行高为单位10     CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : font} context:nil];11     12     return rect.size.height;13 }

  第二步:设置 Cell 的高度

1 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {2     3     CGFloat textHeight = [Tool textHeightWithText:self.myModel.textString font:[UIFont systemFontOfSize:20]];4 5     return textHeight;6 }

   ② 图片自适应高度:根据图片宽度进行等比例缩放

  第一步:修改imageView的高度

1 - (void)layoutSubviews {2     // 1.获取图片的高度3     CGFloat imageH = [Tool imageHeightWithImage:self.myImageView.image];4     // 2.修改imageView的高度5     self.myImageView.frame = CGRectMake(0, textHeight, [UIScreen mainScreen].bounds.size.width, imageH);6 }

  获取图片高度的方法(一般封装在工具类中)

1 + (CGFloat)imageHeightWithImage:(UIImage *)image { 2      3     // 获取图片的宽度和高度 4     CGFloat width = image.size.width; 5     CGFloat height = image.size.height; 6     // 计算图片高度 7     float scile = height / width; 8     float screenH = [UIScreen mainScreen].bounds.size.width; 9 10     return  scile * screenH;11 }

  第二步:设置 Cell 的高度

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {        CGFloat imageHeight = [Tool imageHeightWithImage:self.myModel.image];        return imageHeight;}

  在上面 获取图片高度的方法 中存在一个 bug , 当计算图片高度的数据类型为CGFloat时, 对于某些图片就会出现程序崩溃

  错误代码:

1 + (CGFloat)imageHeightWithImage:(UIImage *)image { 2      3     // 获取图片的宽度和高度 4     CGFloat width = image.size.width; 5     CGFloat height = image.size.height; 6     // 计算图片高度 7     CGFloat scile = height / width; 8     CGFloat screenH = [UIScreen mainScreen].bounds.size.width; 9 10     return  scile * screenH;11 }

  错误信息:

  ③ 两种使用同时出现

   其他部分全部相同,处理在第二步设置Cell的高度, 代码如下:

1 // 设置cell的高度2 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {3     // 得到文字自适应高度4     CGFloat textHeight = [Tool textHeightWithText:self.myModel.textString font:[UIFont systemFontOfSize:20]];5     // 得到图片自适应高度6     CGFloat imageHeight = [Tool imageHeightWithImage:self.myModel.image];7     // 返回两者的和8     return textHeight + imageHeight;9 }

 

   

转载于:https://www.cnblogs.com/gfxxbk/p/5418280.html

你可能感兴趣的文章
Ubuntu 下新建用户
查看>>
gulp配置
查看>>
linux命令截取文件最后n行(所有命令)
查看>>
linux提取指定列字符并打印所有内容(awk)
查看>>
减治算法求n个数中的最小数的位置
查看>>
css3学习 理论之文本
查看>>
Linux 安装python3.7.0
查看>>
<Linux命令行学习 第二节> CentOS - 远程登录管理工具
查看>>
[转]BEHAVOUR TREE2
查看>>
深入理解计算机操作系统(十)
查看>>
XML和Schema命名空间详解
查看>>
npm ERR! Unexpected end of JSON input while parsing near '...inimist":"^1.2.0"}
查看>>
Mybatis Generator逆向工程的使用
查看>>
设计模式(八)_门面模式
查看>>
BFS - 水题
查看>>
软件面试常见题目(转帖)
查看>>
[LeetCode] NO. 387 First Unique Character in a String
查看>>
理解管理信息系统
查看>>
UVA 11991 - Easy Problem from Rujia Liu?
查看>>
CF1101E Polycarp's New Job
查看>>