2015-08-28 91 views
0

我一直在試圖讓一個非常基本的自我尺寸的UITableViewCell出現在表中,但自定義單元甚至不會顯示出來。自定義UITableViewCell永遠不會出現AutoLayout自我尺寸

我正在使用iOS 8,而不用擔心實現委託高度函數的結果。我使用默認的UITableViewCell時,我的UITableView的代碼工作,所以我假設問題出在我的UITableViewCell中,但它很簡單,我不知道如何可能是這種情況。

下面是其約束的單元格(在內容視圖 - 我試圖調整優先級升/降,但一切都沒有幫助): Very bright label

這裏是在TableViewController筆尖加載代碼:

[submissionTable registerNib: [UINib nibWithNibName:@"SimpleCell" bundle:nil] forCellReuseIdentifier:@"SimpleCell"]; 

// These two lines are required for autolayout sizing: 
submissionTable.rowHeight = UITableViewAutomaticDimension; 
submissionTable.estimatedRowHeight = 44; 

這裏是對的UITableViewCell的代碼(我說這很簡單!)

class SimpleCell: UITableViewCell { 
    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
    } 

    override func setSelected(selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 
     // Configure the view for the selected state 
    } 

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: style, reuseIdentifier:reuseIdentifier); 
     self.setTranslatesAutoresizingMaskIntoConstraints(false) 
    } 

    required init(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder); 
     self.setTranslatesAutoresizingMaskIntoConstraints(false) 
    } 
} 

決賽這裏是相當令人沮喪的(和意想不到的)結果: Huge depressing table cell

具有以下不可滿足的限制作爲兩個錯誤。問題是,這些看起來很簡單的滿足:

"<NSLayoutConstraint:0x7aef78b0 UITableViewCellContentView:0x7a79fae0.trailingMargin == UILabel:0x7ae36b10'Label label label label l...'.trailing>", 
"<NSLayoutConstraint:0x7aef78e0 UILabel:0x7ae36b10'Label label label label l...'.leading == UITableViewCellContentView:0x7a79fae0.leadingMargin>", 
"<NSLayoutConstraint:0x7a94db00 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x7a79fae0(0)]>" 


"<NSLayoutConstraint:0x7aef6250 UITableViewCellContentView:0x7a79fae0.bottomMargin == UILabel:0x7ae36b10'Label label label label l...'.bottom>", 
"<NSLayoutConstraint:0x7aef6280 UILabel:0x7ae36b10'Label label label label l...'.top == UITableViewCellContentView:0x7a79fae0.topMargin>", 
"<NSLayoutConstraint:0x7a94db30 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x7a79fae0(0.5)]>" 

注意/編輯: 我接受下面的答案,因爲1#點。由於某種原因,setTranslatesAutoresizingMaskIntoConstraints(false)實際上是打破了它。

+0

Objective-C或swift? – Loxx

+0

我看到你有兩個代碼,是視圖控制器objc,我猜單元格很快?或者是你的objc在錯誤上方顯示? – Loxx

+0

你使用故事板或xib –

回答

0
  1. 如果您在xib中使用自動佈局,那麼這些代碼是不明智的。

    self.setTranslatesAutoresizingMaskIntoConstraints(false)。

  2. 我已經發現,H:[UITableViewCellContentView:0x7a79fae0(0)]>」,

這意味着你的內容查看的高度爲0
可能出錯了CellHeight方法
嘗試。實現TableViewDelegate返回單元格的高度而不是使用UITableViewAutomaticDimension

+0

我不想實現委託來計算行高度,這是使用UITableViewAutomatic維度完成它的全部要點。看起來我應該爲這個視圖設置translatesAutoResizingMaskIntoConstraints,因爲它是唯一從這個xib加載的,並且沒有自動設置(我沒有檢查)。 – JustinHK

+0

非常奇怪的是,如果在單元格上translatesAutoresizingMaskIntoConstraints是** true **,即使它在內置的UITableViewCell上是錯誤的,它也可以工作。咦!?所以這條線不僅是沒有必要的,而是造成了問題。古怪和好奇。 – JustinHK