我一直在閱讀其他人的解決方案,我在這裏找到的具有動態高度和多行標籤的單元格,但沒有一個看起來在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
所有對象之間的約束看的權利,但對於多標籤計算高度是不是不正確的第一個字符串。
有沒有人遇到這個問題,並有一個解決方案?
謝謝。
我也試圖做iOS6的和iOS7方式在返回的的tableView細胞高度:heightForRowAtIndexPath:'[cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize]' –
你可以保持和所設置的'bodyLabel height'更新約束到'sizeThatFits:'每當文字發生變化時 – kezi
感謝@kdogisthebest,我只是試過了你的建議,結果是一樣的。在我看來,計算標籤高度的框架的任何函數在某些情況下爲多行標籤提供了較短的高度(我也嘗試了NSString方法:'boundingRectWithSize:options:attributes:context:') –