我試圖以編程方式創建UITableView(即NO Storyboards/Interface Builder)。我有一個Notices列表(一個簡單的模型,包含標題,作者和一些內容等數據)。我希望這些行能夠垂直動態調整大小(我只爲iOS> = 8.0發佈,所以UITableViewAutomaticDimension是我行的高度)。現在,行應該非常簡單。有三個UILabels。其中每一個都按順序顯示標題,作者和內容之一,並且每個都佈置在另一個之上;然而,我不斷收到警告:「檢測到約束含糊不清地表明零高度的情況」。我理解這意味着我的LayoutConstraints設置不正確;然而,我已經嘗試過所有我能想到的約束組合,而且似乎沒有什麼能解決這個警告!由於AutoLayout,iOS/Swift中的動態行高降爲0
這裏是小區的定義:
import UIKit
public class NoticeCellView : UITableViewCell {
public static let NOTICE_CELL_REUSE_IDENTIFIER = "noticeCell"
private let titleLabel = UILabel()
private let authorLabel = UILabel()
private let contentLabel = UILabel()
public func setNotice(notice: Notice) {
titleLabel.text = notice.getTitle()
authorLabel.text = notice.getAuthorFullName()
contentLabel.text = notice.getContent()
}
public override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.contentView.translatesAutoresizingMaskIntoConstraints = false
titleLabel.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(titleLabel)
authorLabel.translatesAutoresizingMaskIntoConstraints = false
self.contentView.addSubview(authorLabel)
contentLabel.translatesAutoresizingMaskIntoConstraints = false
contentLabel.numberOfLines = 0;
self.contentView.addSubview(contentLabel)
self.addConstraints([
NSLayoutConstraint(
item: self.contentView,
attribute: .Top,
relatedBy: .Equal,
toItem: titleLabel,
attribute: .Top,
multiplier: 1,
constant: 0
),
NSLayoutConstraint(
item: titleLabel,
attribute: .Bottom,
relatedBy: .Equal,
toItem: authorLabel,
attribute: .Top,
multiplier: 1,
constant: 0
),
NSLayoutConstraint(
item: authorLabel,
attribute: .Bottom,
relatedBy: .Equal,
toItem: contentLabel,
attribute: .Top,
multiplier: 1,
constant: 0
),
NSLayoutConstraint(
item: contentLabel,
attribute: .Bottom,
relatedBy: .Equal,
toItem: self.contentView,
attribute: .Bottom,
multiplier: 1,
constant: 0
),
NSLayoutConstraint(
item: self.contentView,
attribute: .Bottom,
relatedBy: .Equal,
toItem: self,
attribute: .Bottom,
multiplier: 1,
constant: 0
),
NSLayoutConstraint(
item: contentLabel,
attribute: .Width,
relatedBy: .LessThanOrEqual,
toItem: self.contentView,
attribute: .Width,
multiplier: 1,
constant: 0
),
])
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
這裏是的UITableViewController代碼的相關片段:
public override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(NoticeCellView.classForCoder(), forCellReuseIdentifier: NoticeCellView.NOTICE_CELL_REUSE_IDENTIFIER)
self.tableView.estimatedRowHeight = 200
}
public override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
public override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return TEST_NOTICES.count
}
public override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(NoticeCellView.NOTICE_CELL_REUSE_IDENTIFIER) as! NoticeCellView
cell.setNotice(TEST_NOTICES[indexPath.row])
return cell
}
澄清:一切工作(視圖佈局正確,與約束條件滿足等),除非動態設置高度。清除有一些限制,我錯過了強制contentView擴展到非零高度。請把我的頭髮拉出來之前告訴我那是什麼限制。
謝謝!
不幸的是,我不認爲這是問題。查看更新的代碼。 –