UItableView的自适应
来源:互联网 发布:编程语言的算法 编辑:程序博客网 时间:2024/06/05 21:18
自适应高度用的比较少,将代码封装好,能够理解就好,自适应高度其实也就是动态设置行高
1.图片高度的自适应
首先要知道在哪个协议方法里写(CGFloat)tableView heightForRow......
在这个方法里,主要计算图片的尺寸,来设置tableview的行高
1.先定义一个image UIImage保存着实际图片的尺寸,而imageView是我们看到的图片的尺寸通过它来设置
2.我们需要等比例缩放,通过图片的实际尺寸和屏幕固定的宽高等比例计算
<span style="font-size:18px;">// 在这个方法里.主要计算图片的尺寸,来设置tableView的行高 UIImage *image = [UIImage imageNamed:self.picArr[indexPath.row]]; // UIImage保存着实际图片的尺寸,而UIImageView是我们看到的图片的尺寸都通过它来设置// NSLog(@"%g", image.size.height);// 获取图片的尺寸 // 通过图片的实际尺寸和屏幕固定的宽进行等比例计算 CGFloat rowHeight = self.view.frame.size.width * image.size.height / image.size.width;</span>
3.之后在layoutSubviews执行方法(这是cell走的最后一个方法,我们需要现在主函数协议里求行高,然后在函数里自适应图片)
<span style="font-size:18px;">UIImage *image = self.picImageView.image; CGFloat height = image.size.height * self.contentView.frame.size.width / image.size.width; self.picImageView.frame = CGRectMake(0, 0, self.contentView.frame.size.width, height);</span>
2.文字的自适应
1.计算文字的高度 首先将字体的大小放到一个数组里
2.根据文本的内容和文本的字体进行计算高度
<span style="font-size:18px;"> // 计算文字的高度 // 将字体大小写到key - value NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName, nil]; // 根据文本的内容和文本的字体进行计算高度 // 参数一:告诉系统,文本显示的最大范围 // CGRect rect = [self.ziArr[indexPath.row] boundingRectWithSize:CGSizeMake(self.view.frame.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];</span>
然后在cell里执行,对lable进行尺寸设置
<span style="font-size:18px;">// 计算文字的高度 // 将字体大小写到key - value NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName, nil]; // 根据文本的内容和文本的字体进行计算高度 // 参数一:告诉系统,文本显示的最大范围 // 这个方法是nsstring的 CGRect rect = [str boundingRectWithSize:CGSizeMake(self.contentView.frame.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil]; // 对lable进行尺寸设置 self.newsLable.frame = CGRectMake(0, height, self.contentView.frame.size.width, rect.size.height);</span>
#pragma mark 设置tableView cell每行的高度- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ // 在这个方法里.主要计算图片的尺寸,来设置tableView的行高 UIImage *image = [UIImage imageNamed:self.picArr[indexPath.row]]; // UIImage保存着实际图片的尺寸,而UIImageView是我们看到的图片的尺寸都通过它来设置// NSLog(@"%g", image.size.height);// 获取图片的尺寸 // 通过图片的实际尺寸和屏幕固定的宽进行等比例计算 CGFloat rowHeight = self.view.frame.size.width * image.size.height / image.size.width; // 计算文字的高度 // 将字体大小写到key - value NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14], NSFontAttributeName, nil]; // 根据文本的内容和文本的字体进行计算高度 // 参数一:告诉系统,文本显示的最大范围 // CGRect rect = [self.ziArr[indexPath.row] boundingRectWithSize:CGSizeMake(self.view.frame.size.width, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil]; return rowHeight + rect.size.height;}
0 0
- UITableview 的自适应高度
- UItableView的自适应
- UITableView的cell自适应高度
- UITableView的图片自适应高度
- UITableView的cell自适应高度
- UITableView 行自适应高度的实现
- UITableView 行自适应高度的实现
- UITableView 行自适应高度的实现
- UITableView 行自适应高度的实现
- UITableView最简单的高度自适应原理
- UItableView cell 自适应高度
- UItableview自适应高度
- UITableView高度自适应
- uitableView高度自适应
- UITableView高度自适应
- uitableView自适应大小
- UITableView行高自适应
- 关于 UITableView 中 cell 自适应高度的问题
- sys/kernel下创建可操作内核文件。
- 计算年龄
- Maven+Jetty运行项目无法热修改html处理
- ListView中一键获取所有EditText的值
- mConUserName为空的问题,值得学习哦
- UItableView的自适应
- C++ CCF真题----消除类游戏
- 单点登录1:安装CAS服务器
- window下安装es
- 华为oj 简单密码破解
- iOS开发 - 统计图的绘制
- eclipse连接mysql ping失败
- Drawable setBounds方法
- openssl pfx文件转cer文件