我將xcode升級到6.3後遇到此問題。使用xcode 6.2編譯的前一版本沒有這個問題,並且效果很好。iOS8中自定義UITableViewCell動態高度的內容具有不同的寬度
問題是:切換到其他選項卡後,或從推繼續回來後,大多數單元格的寬度會縮小一點點。但有些不。它看起來像以下
單元格第二行中的標籤內容具有正確的寬度。所有單元格在首次啓動後都會以正確的寬度顯示內容。 在上圖中,有兩個uitableviewcells。 內容視圖:藍色; 背景視圖:淡綠色; 內容標籤:紫色;
第1個單元格中的bg視圖和contentLabel縮小。這就是我不想要的。
我使用故事板來設置約束。 bg視圖的約束。 labelview的約束條件 我也嘗試緩存單元格高度根據這個問題UITableView layout messing up on push segue and return. (iOS 8, Xcode beta 5, Swift),但它不起作用。
//In viewDidLoad
self.tableView.estimatedRowHeight = 100.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.contentInset = UIEdgeInsetsZero;
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
float height = [[self.estimatedRowHeightCache objectForKey:[NSString stringWithFormat:@"%ld", indexPath.row]] floatValue];
if (height) {
return height;
} else {
return UITableViewAutomaticDimension;
}
}
//cache row height. I observed this doesn't been called in first launching in debugging.
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
[self.estimatedRowHeightCache setValue:[NSNumber numberWithFloat:cell.frame.size.height] forKey:[NSString stringWithFormat:@"%ld", indexPath.row]];
}
感謝您的幫助!
你可以設置背景色的單元格的內容查看?或者設置單元格的背景顏色,或者你已經設置好並檢查了? –
我爲每個視圖設置了背景顏色。 contentView:藍色;背景視圖:淡綠色,這是添加到contentView中的視圖作爲標籤背景。 contentLabel:紫色。 – AngleZhou
你能分享你的代碼,你設置這些顏色?另外,你用xib嗎? –