UITableViewCell高度自适应.docx

  1. 1、本文档共6页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 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

文档评论(0)

linsspace + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档