Which is the best approach among Autolayout or calculating the height using NSAttributedString, to implement dynamic height of an UITableViewCell?

拥有回忆 提交于 2019-12-23 19:13:04

问题


I have followed the tutorial from http://raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout and implemented the same, but with the release of iOS 8.3, the above tutorial doesn't seem to be working.

Therefore i switched back to the following code which works absolutely fine

-(float)height :(NSMutableAttributedString*)string
{
  NSAttributedString *attributedText = string;

  CGRect rect = [attributedText boundingRectWithSize:CGSizeMake(200, MAXFLOAT)
                                           options:(NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading)
                                           context:nil];
  CGSize requiredSize = rect.size;
  return requiredSize.height;
}

and call the same in

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{   
  CGFloat heightOfcell = [self height:[array objectAtIndex:indexPath.row]];
  return heightOfcell;
}

Can someone suggest a better way for doing the same in auto-layout and i am not using StoryBoard?


回答1:


I would suggest use Autolatyout on cell's content view and give an Estimated Row Height in viewDidLoad like this self.tableView.estimatedRowHeight = 200; However this apporach may not support the Earlier versions of IOS.



来源:https://stackoverflow.com/questions/29563378/which-is-the-best-approach-among-autolayout-or-calculating-the-height-using-nsat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!