使用AutoLayout动态计算UITableViewCell高度
2014-10-13 23:44
162 查看
公司最近要用到autoLayout,今天看了一些autoLayout相关的东西。临下班的时候,一个同事问到如何使用autoLayout实现动态计算UITableViewCell高度,于是一起研究了一番,参考了一篇动态计算UITableViewCell高度详解文章,回家简单实现了使用autoLayout实现了动态计算UITableViewCell高度。
实现上面的步骤其实很简单:
1、在Cell对应的xib文件中建立完整的约束。
2、使用[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]方法对cell进行动态计算高度,然后在- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath返回cell的高度。
注意:[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]一定是cell.contentView,不能用cell。
相关代码:
http://www.cocoachina.com/industry/20140604/8668.html
http://www.ifun.cc/blog/2014/02/21/dong-tai-ji-suan-uitableviewcellgao-du-xiang-jie/
代码链接:http://download.csdn.net/detail/lcg0412/8034675
实现上面的步骤其实很简单:
1、在Cell对应的xib文件中建立完整的约束。
2、使用[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]方法对cell进行动态计算高度,然后在- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath返回cell的高度。
注意:[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]一定是cell.contentView,不能用cell。
相关代码:
- (void)viewDidLoad { [super viewDidLoad]; _data = @[@"我的境况放假", @"jkdjfkadjfdjafj的积分卡积分卡大家反馈到敬爱放大家分开阿娇快放假啊积分卡积分积分开始就发的是减肥打手机发电视剧发的是开放的是的空间按附件的垃圾费看得见啊",@"jkdjfka的境况放假大放假打卡机飞就看得见反馈的减肥", @"jkdjajfldajklf 贷记卡家乐福大家快乐防静电阿里的积分大姐夫", @"放假啊考虑放假了案件反馈的安家费看大家分开打反馈到敬爱放的骄傲看反馈卡就飞快的放假", @"jkdja将开发大姐夫就打开附件安定放假开打放假的卡风较大飞交罚款的姐夫"]; _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; [self.view addSubview:_tableView]; _cell = [[[NSBundle mainBundle] loadNibNamed:@"PrCell" owner:self options:nil] lastObject]; _textViewCell = [[[NSBundle mainBundle] loadNibNamed:@"PrTextViewCell" owner:self options:nil] lastObject]; _textViewCell.textView.delegate = self; // Do any additional setup after loading the view, typically from a nib. } #pragma mark UITableView delegate - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return _data.count+5; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ if(indexPath.row < _data.count){ PrCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PrCell"]; if(cell == Nil){ cell = [[[NSBundle mainBundle] loadNibNamed:@"PrCell" owner:self options:nil] lastObject]; } cell.content = _data[indexPath.row]; return cell; } else{ PrTextViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PrTextViewCell"]; if(cell == nil){ cell = [[[NSBundle mainBundle] loadNibNamed:@"PrTextViewCell" owner:self options:nil] lastObject]; cell.textView.delegate = self; } return cell; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ if(indexPath.row < _data.count){ _cell.content = _data[indexPath.row]; CGSize size = [_cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; return size.height+1.0f; } else{ CGSize size = [_textViewCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; _textViewCell.textView.text = _inputText; CGSize textViewSize = [_textViewCell.textView sizeThatFits:CGSizeMake(_textViewCell.textView.frame.size.width, 100000.0f)]; return 50 > size.height+1.0f+textViewSize.height ? 50 : size.height+1.0f+textViewSize.height; } } #pragma mark UITextView delegate - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ if ([text isEqualToString:@"\n"]) { NSLog(@"h=%f", textView.contentSize.height); } return YES; } - (void)textViewDidChange:(UITextView *)textView{ _inputText = textView.text; [_tableView beginUpdates]; [_tableView endUpdates]; }参考:
http://www.cocoachina.com/industry/20140604/8668.html
http://www.ifun.cc/blog/2014/02/21/dong-tai-ji-suan-uitableviewcellgao-du-xiang-jie/
代码链接:http://download.csdn.net/detail/lcg0412/8034675
相关文章推荐
- 使用了Autolayout的UITableViewCell动态布局和高度动态改变自适应
- 使用AutoLayOut技术告别UITableViewCell高度计算
- UITableView使用AutoLayout动态计算cell高度
- 【转】动态计算UITableViewCell高度详解
- 动态计算UITableViewCell高度详解
- 动态计算自定义 UITableViewCell 的高度
- 动态计算UITableViewCell高度详解
- Auto Layout 使用心得(五)—— 根据文字、图片自动计算 UITableViewCell 高度
- Autolayout和Storyboard中动态UITableViewCell的高度
- 如何利用autolayout动态计算UITableViewCell的高度
- xib 动态计算UITableViewCell高度
- 动态计算UITableViewCell高度详解
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 动态计算UITableViewCell高度详解
- 动态计算UITableViewCell高度详解 (转)
- iOS 动态计算UITableViewCell 高度 以及计算UILable 高度 自定义Cell
- [译]使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- 使用Autolayout实现UITableView的Cell动态布局和高度动态改变
- UITableViewCell使用自动布局计算高度