ios15使用纯代码计算cell的高度
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                ios15使用纯代码计算cell的高度
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                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的高度的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 文件结构
- 下一篇: 基于AFNetworking的封装的工具
