2017-02-03 38 views
0

快速的問題:我有一個UITableViewController,一般設置工作正常。 我只是試圖微調間距。UITableViewController estimatedRowHeight with detailTextLabel

UITableViewControllerviewDidLoad我打電話:

tableView.rowHeight = UITableViewAutomaticDimension 
tableView.estimatedRowHeight = 60 

我的一些默認細胞的使用detailTextLabelstyle: .subtitle)和一些不(style: .default)。 當他們這樣做時,單元格似乎不會調整行高度。 文字看起來很緊張,特別是當detailTextLabel是兩行時。

我想避免必須重寫tableView:heightForRowAtIndexPath:函數? 有沒有一招?

謝謝

+0

發生了什麼事情的快速屏幕? :) –

+0

而不是使用height屬性方法嘗試與高度委託方法相同。 Bcz委託方法調用每個單元格,因此可以爲每個單元格設置不同的高度。 –

+0

使用約束。和委託方法。 –

回答

1

這是系統單元的默認行爲。如果您期望獲得另一個輸出,只需創建您的自定義單元格,並根據需要給出頂部和底部約束。祝你好運;)

+0

我試過這個,但是當我設置'topAnchor'和'bottomAnchor'時,標籤移動了,但是單元格高度沒有改變。 – Joseph

+0

你對細胞做了哪些定製?對於我所說的自定義單元格,使用自己的標籤的單元格和對這兩個標籤都設置的約束。當有正確的約束時,單元格會正確調整大小。 –

+0

好的 - 所以我不應該在我的自定義UITableViewCell中使用'self.textLabel'和'self.detailTextLabel'? – Joseph

0

我遇到了同樣的問題,我得到了一個最佳答案。我以編程的方式添加了約束,它的功能對我來說很有用。希望它能幫助解決你的問題。

- (void)awakeFromNib{ 
    [super awakeFromNib]; 
    for (NSLayoutConstraint *cellConstraint in self.constraints) { 
     [self removeConstraint:cellConstraint]; 
     id first_object = cellConstraint.first_object == self ? self.contentView : cellConstraint.first_object; 
     id seccondItem = cellConstraint.second_object == self ? self.contentView : cellConstraint.second_object; 
     NSLayoutConstraint *contentViewConstraint = 
     [NSLayoutConstraint constraintWithItem:first_object 
           attribute:cellConstraint.firstAttribute 
           relatedBy:cellConstraint.relation 
            toItem:seccondItem 
           attribute:cellConstraint.secondAttribute 
           multiplier:cellConstraint.multiplier 
            constant:cellConstraint.constant]; 
     [self.contentView addConstraint:contentViewConstraint]; 
    } 
} 
+0

也許我需要刪除現有的約束。首先會嘗試。 – Joseph

+0

讓我知道,如果刪除約束條件將適用於您。 – Nirmalsinh

+0

nope - 單元格高度不受影響。 – Joseph