日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/12/18 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ios15使用纯代码计算cell的高度 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

ios15使用純代碼計(jì)算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的高度計(jì)算";// 去除tableView的默認(rèn)下劃線self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;self.tableView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:0.9];// 注冊cell[self.tableView registerClass:[MTableViewCell class] forCellReuseIdentifier:ID];// 異步獲取數(shù)據(jù)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];}// 造數(shù)據(jù)// [self.dataSource addObjectsFromArray:self.dataSource];// 在主線程中刷新數(shù)據(jù)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的估計(jì)高度,主要目的是優(yōu)化cell高度的計(jì)算次數(shù)*/ - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {return 200; }/*** 初始化數(shù)據(jù)*/ - (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];// 文字內(nèi)容UILabel *labelContent = [[UILabel alloc] initWithFrame:CGRectMake(margin, 0, SCWIDTH - 20 , 30)];labelContent.top = imageIcon.bottom + margin;// 設(shè)置顯示多行文字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;// 設(shè)置用戶頭像self.imageIcon.image = [UIImage imageNamed: dataModel.icon];// 設(shè)置用戶名self.labelName.text = dataModel.name;// 計(jì)算文字內(nèi)容的高度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;// 設(shè)置圖片內(nèi)容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

總結(jié)

以上是生活随笔為你收集整理的ios15使用纯代码计算cell的高度的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。