- 1、本文档共6页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
UITableViewCell高度自适应
在iOS开发过程中,UITableView是应用最为广泛的控件之一,而在实现过程中,如何动态的去计算每个单元格的高度往往会纠结着我们,我也遇到过同样的问题,经过多次的测试和阅读大量的资源,大概知道集中高度自适应的方法,它们归根到底还是对AutoLayout的应用。方法一:使用xib进行布局,添加约束;(不常用)二:代码手写,我在工程中使用的是Masonry添加约束,值得一提的是不管哪一种方式都要保证有一个控件能够撑起cell格。下面直接上代码。这里的viewController.h来初始化表视图,#import "ViewController.h"#import "Masonry.h"#import "JC_TableViewCell.h"#import "TestModel.h"@interfaceViewController()<UITableViewDelegate, UITableViewDataSource>@property (nonatomic, strong) UITableView *tableView_jc;@end@implementationViewController- (void)viewDidLoad { [superviewDidLoad];self.tableView_jc = [[UITableViewalloc] initWithFrame:CGRectZerostyle:UITableViewStylePlain];self.tableView_jc.delegate = self;self.tableView_jc.dataSource = self;_tableView_jc.backgroundColor = [UIColorgrayColor];self.tableView_jc.tableFooterView = [[UIViewalloc]init]; [self.viewaddSubview:_tableView_jc]; [_tableView_jcmas_makeConstraints:^(MASConstraintMaker *make) {make.top.equalTo(_tableView_jc.superview).with.offset(20);make.left.equalTo(_tableView_jc.superview.mas_left);make.right.equalTo(_tableView_jc.superview.mas_right);make.bottom.equalTo(_tableView_jc.superview.mas_bottom); }];self.tableView_jc.estimatedRowHeight = 40;//估算高度self.tableView_jc.rowHeight = UITableViewAutomaticDimension;}- (NSInteger)tableView:(UITableView*)tableViewnumberOfRowsInSection:(NSInteger)section{return3;}- (UITableViewCell*)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath{JC_TableViewCell *testCell = [tableViewdequeueReusableCellWithIdentifier:@"cell"];if (testCell == nil) {testCell = [[JC_TableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"cell"];testCell.backgroundColor = [UIColorcolorWithRed:arc4random() % 10 * 0.1green:arc4random() % 10 * 0.1blue:arc4random() % 10 * 0.1alpha:1.0]; }testCell.selectionStyle = UITableViewCellSelectionStyleNone;TestModel *model = [[TestModelalloc]init];if (indexPath.row == 0) { model.name = @"numn.1";model.introduction = @"123456
1亿VIP精品文档
相关文档
最近下载
- 浙江省统计调查大赛调研报告获省一.pdf
- 党课课件(精美PPT).ppt VIP
- 2024年生物中考读图理解题专题训练:生物圈中的动物.docx VIP
- 并网光伏电站电能质量分析.docx
- 小学生主题班会 珍爱生命 远离毒品 课件(共24张PPT).pptx VIP
- 大力推进马克思主义中国化、时代化、大众化专用课件.ppt VIP
- 2021-2022学年上海九年级数学相似三角形专题训练 专题06 相似三角形的性质重难点专练(解析版).docx VIP
- DB42_T 1763-2021 高速公路服务区(停车区)服务设施规范.docx
- 保密要害部门部位采取防护措施.docx
- 湛江市2024年高中学考-通用技术 题库(理论考试参考材料)题库.pdf
文档评论(0)