2015-04-12 113 views
0

我將xcode升級到6.3後遇到此問題。使用xcode 6.2編譯的前一版本沒有這個問題,並且效果很好。iOS8中自定義UITableViewCell動態高度的內容具有不同的寬度

問題是:切換到其他選項卡後,或從推繼續回來後,大多數單元格的寬度會縮小一點點。但有些不。它看起來像以下enter image description here

單元格第二行中的標籤內容具有正確的寬度。所有單元格在首次啓動後都會以正確的寬度顯示內容。 在上圖中,有兩個uitableviewcells。 內容視圖:藍色; 背景視圖:淡綠色; 內容標籤:紫色;
第1個單元格中的bg視圖和contentLabel縮小。這就是我不想要的。

我使用故事板來設置約束。 bg視圖的約束。 bg view's contraints labelview的約束條件 enter image description here 我也嘗試緩存單元格高度根據這個問題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]]; 
} 

感謝您的幫助!

+0

你可以設置背景色的單元格的內容查看?或者設置單元格的背景顏色,或者你已經設置好並檢查了? –

+0

我爲每個視圖設置了背景顏色。 contentView:藍色;背景視圖:淡綠色,這是添加到contentView中的視圖作爲標籤背景。 contentLabel:紫色。 – AngleZhou

+0

你能分享你的代碼,你設置這些顏色?另外,你用xib嗎? –

回答

0

那麼你沒有設置水平空間約束爲您的contentView頂部的視圖。你只爲你的標籤和超視圖設置水平空間限制。

做這個

  1. 選擇包含標籤
  2. 編輯器視圖>針>前導空格,以上海華
0

實現DataSource的方法泰伯維WillDisplayCell

也可以在細胞類實現(如果它的自定義單元格)方法 - (無效)awakeFromNib有設置單元格邊框。

相關問題