ios15使用纯代码计算cell的高度

#import "MTableViewController.h"
#import "MTableViewCell.h"
#import "DataModel.h"static NSString *ID = @"cell";@interface MTableViewController ()@property (nonatomic, strong) NSMutableArray *dataSource;@end@implementation MTableViewController- (void)viewDidLoad {[super viewDidLoad];self.title = @"cell的高度计算";// 去除tableView的默认下划线self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;self.tableView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.9];// 注册cell[self.tableView registerClass:[MTableViewCell class] forCellReuseIdentifier:ID];// 异步获取数据dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{NSString *path = [[NSBundle mainBundle] pathForResource:@"cellList.plist" ofType:nil];NSArray *array = [NSArray arrayWithContentsOfFile:path];for (NSDictionary *dict in array) {DataModel *dm = [DataModel initWith:dict];[self.dataSource addObject:dm];}// 造数据// [self.dataSource addObjectsFromArray:self.dataSource];// 在主线程中刷新数据dispatch_async(dispatch_get_main_queue(), ^{[self.tableView reloadData];});});
}#pragma mark - Table view data source- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return self.dataSource.count;
}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {MTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];cell.selectionStyle = UITableViewCellSelectionStyleNone;cell.dataModel = self.dataSource[indexPath.row];return cell;
}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {DataModel *dm = self.dataSource[indexPath.row];return dm.cellHeight;
}/***  给出cell的估计高度,主要目的是优化cell高度的计算次数*/
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {return 200;
}/*** 初始化数据*/
- (NSMutableArray *)dataSource {if (_dataSource == nil) {_dataSource = [NSMutableArray array];}return _dataSource;
}@end
#import "MTableViewCell.h"
#import "UIView+Expand.h"
#import "DataModel.h"#define SCWIDTH [UIScreen mainScreen].bounds.size.width
#define SCHEIGHT [UIScreen mainScreen].bounds.size.heightstatic CGFloat const margin = 10;@interface MTableViewCell()@property (nonatomic, weak) UIImageView *imageIcon;
@property (nonatomic, weak) UILabel *labelName;
@property (nonatomic, weak) UILabel *labelContent;
@property (nonatomic, weak) UIImageView *picView;
@end@implementation MTableViewCell- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];if (self) {[self setUpView];}return self;
}- (void)setUpView {// 用户头像UIImageView *imageIcon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];imageIcon.left = margin;imageIcon.top = margin;self.imageIcon = imageIcon;[self.contentView addSubview:imageIcon];// 用户名CGFloat nameW = SCWIDTH - imageIcon.width - 3 * margin;UILabel *labelName = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, nameW, 30)];labelName.left = imageIcon.right + margin;labelName.top = margin;labelName.font = [UIFont systemFontOfSize:16];self.labelName = labelName;[self.contentView addSubview:labelName];// 文字内容UILabel *labelContent = [[UILabel alloc] initWithFrame:CGRectMake(margin, 0, SCWIDTH - 20 , 30)];labelContent.top = imageIcon.bottom + margin;// 设置显示多行文字labelContent.lineBreakMode = NSLineBreakByCharWrapping;labelContent.numberOfLines = 0;labelContent.font = [UIFont systemFontOfSize:15];self.labelContent = labelContent;[self.contentView addSubview:labelContent];// 图片UIImageView *picView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 120, 90)];picView.left = margin;self.picView = picView;[self.contentView addSubview:picView];
}- (void)setFrame:(CGRect)frame {frame = CGRectMake(frame.origin.x, frame.origin.y + 10, frame.size.width, frame.size.height - 10);[super setFrame:frame];
}- (void)setDataModel:(DataModel *)dataModel {_dataModel = dataModel;// 设置用户头像self.imageIcon.image = [UIImage imageNamed: dataModel.icon];// 设置用户名self.labelName.text = dataModel.name;// 计算文字内容的高度CGFloat height =  [dataModel.text boundingRectWithSize:CGSizeMake(SCWIDTH - 2 * margin, CGFLOAT_MAX)options:NSStringDrawingUsesLineFragmentOriginattributes:@{NSFontAttributeName : self.labelContent.font}context:nil].size.height;self.labelContent.height = height;self.labelContent.text = dataModel.text;// 设置图片内容if (dataModel.picture) {self.picView.hidden = NO;self.picView.top = self.labelContent.bottom + margin;self.picView.image = [UIImage imageNamed:dataModel.picture];dataModel.cellHeight = self.picView.bottom + 2 * margin;} else {self.picView.hidden = YES;dataModel.cellHeight = self.labelContent.bottom + 2 * margin;}}@end

主要源码
https://e.coding.net/lujun1/afnetworkinggetdemo/testAutoCellHeight.git

ios15使用纯代码计算cell的高度相关推荐

  1. iOS开发总结-UITableView 自定义cell和动态计算cell的高度

    UITableView cell自定义头文件: shopCell.h #import <UIKit/UIKit.h> @interface shopCell : UITableViewCe ...

  2. IOS15使用Masonry和自动计算Cell的高度

    IOS15使用Masonry和自动计算Cell的高度 核心源码 // 步骤1:tableView.rowHeight = UITableViewAutomaticDimension;// 步骤2:ta ...

  3. 纯代码计算不等高cell

    不等高cell纯代码: 对应的GitHub 项目链接:https://github.com/liminting/CalculateCellDemo 不等高cell - 先设置高度rowH = 250 ...

  4. (素材源码)猫猫学IOS(十七)UI之纯代码自定义Cell实现新浪微博UI

    猫猫分享,必须精品 素材代码地址:http://download.csdn.net/detail/u013357243/8580249 原文地址:http://blog.csdn.net/u01335 ...

  5. 猫猫学IOS(十七)UI之纯代码自定义Cell实现新浪微博UI

    猫猫分享,必须精品 素材代码地址:http://blog.csdn.net/u013357243/article/details/44976175 原文地址:http://blog.csdn.net/ ...

  6. iOS开发UI篇—以微博界面为例使用纯代码自定义cell程序编码全过程(三·完结)...

    一.需要改进的地方 还需改进的地方:cell的高度需要根据每条微博的数据进行动态设置. 设置cell的高度可以有两种方式,一种是通过rowheight属性来进行设置,一种是通过代理来进行设置.通过属性 ...

  7. iOS开发UI篇—以微博界面为例使用纯代码自定义cell程序编码全过程(二)

    说明: 该部分完成对自定义cell页面的基本搭建,尚未进行优化处理.且还存在很多问题,譬如每行的高度设置暂时是固定的,这些问题将会在下一篇文中解决. 一.实现效果 二.实现代码 数据模型部分: YYw ...

  8. iOS8新特性 计算 cell 的高度

    http://tutuge.me/2015/08/08/autolayout-example-with-masonry2/ 1.tableview: 自动计算 tableVIew 的 cell 的高度 ...

  9. 1014-34-首页15-计算原创微博的frame------计算cell的高度---计算 UILabel 的 CGSize 的方法...

    一.总体思路: 在控制器中,每次拿到数据模型(请求了数据.加载新微博)的时候,就调用 - (NSArray *)stausFramesWithStatuses:(NSArray *)statuses, ...

最新文章

  1. 使用.NET REACTOR制作软件许可证
  2. 在RichTextEditor组件中使用自定义右键。
  3. kube-scheduler 磁盘调度源码分析
  4. 软件设计师学习3——操作系统知识
  5. 【问题待解决】自定义控件设计界面报错,编译运行正常
  6. RDkit:介绍smiles编码,smart编码及摩根指纹(ECFP)
  7. 迈高图手机版_迈高图(地图数据下载器)
  8. Excel 数据随机分组
  9. IP is locked 的解决办法 Vivado
  10. Linux下安装MySQL、安装注意事项以及安装问题解决等(以腾讯云服务为主)
  11. VR全景制作方法教程完整版
  12. 设计模式-中介者模式
  13. Kinect使用系列
  14. 计算机网络:小明在家打开一个网址过程细致版(DNS缓存、DNS查询、TCP/IP协议、ARP协议、HTML渲染)
  15. 在外包公司工作是什么样的体验?
  16. windows切换窗口,取消edge窗口为多个
  17. 防抖debounce立即防抖和延时防抖(二)
  18. 第979期机器学习日报(2017-05-24)
  19. CSS 关键字 initial、inherit 和 unset
  20. 移动增值业务相关知识

热门文章

  1. 凹凸世界服务器维护到几点,《凹凸世界》2021年7月21日更新版本停服维护公告...
  2. Android消息向下堆积,android - 从CoordinatorLayout中的按钮单击事件触发NestedScroll折叠动作 - 堆栈内存溢出...
  3. jpush推送格式 swift_Swift中配置极光推送
  4. 砰的一声,实验室又炸鸡了
  5. C语言中三块“难啃的硬骨头”
  6. 基于 xilinx vivado 的PCIE ip核设置与例程代码详解
  7. Python EFZ文件 气象_python的日常应用-gt;入门篇01
  8. python读取sqlserver数据库方法_SQLServer数据库之Python读取配置文件,并连接数据库SQL Server...
  9. 大学python搜题app_2021年中国大学MOOC的APP用Python玩转数据答案搜题公众号
  10. Can you answer these queries I SPOJ - GSS1 (线段树维护区间连续最大值/最大连续子段和)...