2015-09-01 22 views
2

我一直在閱讀其他人的解決方案,我在這裏找到的具有動態高度和多行標籤的單元格,但沒有一個看起來在100%工作。UITableViewCell中的多行UILabel在iOS8中給出與autoLayout錯誤的高度

所述細胞具有以下垂直約束:Vertical cell constraints

的的tableView被配置爲:

self.tableView.estimatedRowHeight = 100; 
self.tableView.rowHeight = UITableViewAutomaticDimension; 

在該方法的tableview:的cellForRowAtIndexPath:我設置多行標籤的preferredMaxLayoutWidth:

JBLabelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JBLabelTableViewCell" forIndexPath:indexPath]; 
NSString *labelText = [self.items objectAtIndex:indexPath.item]; 
[cell.bodyLabel setText:labelText]; 
CGRect cellFrame = CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), 99999); 
[cell setBounds:cellFrame]; 
cell.contentView.bounds = cell.bounds; 
[cell layoutIfNeeded]; 
[cell.bodyLabel setPreferredMaxLayoutWidth:CGRectGetWidth(cell.bodyLabel.frame)]; 
return cell; 

dataSource設置了這兩個字符串:

_items = @[@"On this screen you can see the groups of people that you have set yourself up to chat with.\n\nCommunities are groups of people that are created for you on the basis of your interests, and how you feel about your condition, or the conditions of those you care about.\n\nThey are not only there to provide you with help or someone to talk to but can also benefit from the help and support that you can give.", 
       @"On this screen you can see a list of the conversations that you are involved in.\n\nWithin conversations you can ask questions to like-minded people or just have someone to chat to. These are places where you can help someone or get someone to help you."]; 

運行在iPhone 6模擬器項目,第一個字符串獲得其最後一行截止:iPhone screenshot

所有對象之間的約束看的權利,但對於多標籤計算高度是不是不正確的第一個字符串。

有沒有人遇到這個問題,並有一個解決方案?

謝謝。

+0

我也試圖做iOS6的和iOS7方式在返回的的tableView細胞高度:heightForRowAtIndexPath:'[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]' –

+0

你可以保持和所設置的'bodyLabel height'更新約束到'sizeThatFits:'每當文字發生變化時 – kezi

+0

感謝@kdogisthebest,我只是試過了你的建議,結果是一樣的。在我看來,計算標籤高度的框架的任何函數在某些情況下爲多行標籤提供了較短的高度(我也嘗試了NSString方法:'boundingRectWithSize:options:attributes:context:') –

回答

1

您的代碼可能correct.i剛纔添加這兩行(沒有必要說了。從你已經加入你的屏幕截圖的樣子!)

cell. bodyLabel.numberOfLines = 0; 
cell. bodyLabel.lineBreakMode = NSLineBreakByWordWrapping; 

它似乎是你的問題在用戶界面打下。這是我用於UITableViewCell的用戶界面,它工作得很好 enter image description here

UPDATE!

只需將其添加到您的viewDidLoad [self.view layoutIfNeeded]; [self.view setNeedsLayout]; in viewDidLoad正在使用用戶界面的框架,您需要更新約束和視圖以使用設備屏幕框架layoutIfNeeded和setNeedsLayout的順序非常重要。您需要先調用layoutIfNeeded。

+1

問題始終沒有發生,這取決於屏幕大小和字符串大小。如果字體類型/大小和約束與我的不同,那麼它可能不會重現。我的項目中有類似的限制。我在這裏分享我使用的示例項目的鏈接,以防萬一任何人想看看:https://www.dropbox.com/s/gpjcdny50m37on3/label.zip?dl=0 –

+0

只需將其添加到您的viewDidLoad [self.view layoutIfNeeded]; [self。查看setNeedsLayout]; in viewDidLoad正在使用用戶界面的框架,您需要更新約束和視圖以使用設備屏幕框架 layoutIfNeeded和setNeedsLayout的順序很重要。你需要先調用layoutIfNeeded – user962913

+0

謝謝@ user962913將這兩行添加到viewDidLoad確實解決了問題。您可以使用修復程序更新您的答案,以便其他人可以更快地找到解決方案。 –

相關問題