1. 程式人生 > >ios根據內容設定cell的高度

ios根據內容設定cell的高度

基本思路是:先獲取該行要顯示的文字內容,根據文字計算行高

// 根據文字高度來設定cell高度
-(CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath{
  NSString *textArray = [_textArray objectAtIndex:indexPath.row];
  NSDictionary *attribute = @{NSFontAttributeName:[UIFont systemFontOfSize:15.0]};
  CGSize textSize1 = [textArray boundingRectWithSize:CGSizeMake(ViewWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine attributes:attribute context:nil
].size; return textSize1.height+35;//讓文字離cell有一定的距離顯示,上7.5,下7.5 }