2014-10-20 53 views
0

你好,我已經實現代碼如下細胞內的一個標籤,標籤的高度是根據內容的改變,所以設置的嘗試後,我試圖通過下面的代碼UITableViewCell的動態身高問題

CGSize cellSize = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]; 
CGFloat height = cellSize.height; 

計算高度,但總是返回高度作爲0,任何人都可以幫我糾正這個問題嗎?

你可以看到這裏的代碼https://github.com/iamabhiee/Github

+0

顯示在其中創建約束 – 2014-10-20 08:55:33

+0

@GrzegorzKrukowski你可以在這裏看到https://github.com/iamabhiee/Github代碼 – Abhishek 2014-10-20 09:36:00

回答

0

使用此代碼

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ 
CGFloat width=[UIScreen mainScreen].bounds.size.width; 
//width - rigth and left constraints 8 
NSString* strText = @"Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum"; 

NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:17] forKey: NSFontAttributeName]; 

CGRect frame = [strText boundingRectWithSize:CGSizeMake(width - 8 - 8, 3000) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:attributesDictionary context:nil]; 
// 16 for bottom and top space 
return frame.size.height + 16; 
} 
+0

謝謝,但我正在尋找一個更好的你的XIB文件或.m文件解決方案,因爲我將有多種類型的tableviewcell – Abhishek 2014-10-20 10:50:56

0

你錯了約束和失蹤底部約束+你錯過了的UILabel首選寬度。 看看我的截圖 - 它修復後的工作。

fix