我正在與控制器簡單的聯繫人應用程序有兩種:ContactTableViewController
和custome細胞:ContactTableViewCell
。我創建了這裏的款式是定製自定義表視圖細胞,identifier
是ContactTableViewCell
,而且類也是ContactTableViewCell
。 小區有兩個UILabel
領域,其中暴露於ContactTableViewCell.swift
類,如下所示:自定義的UITableViewCell只顯示一個的UILabel,即使在故事板
import UIKit
class ContactTableViewCell: UITableViewCell {
@IBOutlet weak var name: UILabel!
@IBOutlet weak var info: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
print("cell loaded with name: ", name)
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
現在,我想在我的ContactViewController
來顯示他們,我的控制器相關的部分看起來像這樣:
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 2
}
// here we communicate with parts of the app that owns the data
override func tableView(_ tableView : UITableView
, cellForRowAt indexPath: IndexPath
) -> UITableViewCell {
let id = "ContactTableViewCell"
// deque a cell as an instance of ContactTableViewCell
guard let cell = tableView.dequeueReusableCell(withIdentifier: id, for : indexPath)
as? ContactTableViewCell else {
fatalError("dequed cell is not instance of ContactTableViewCell")
}
cell.name.text = "hello"
cell.info.text = "hello information"
然而,當我運行仿真我只看到「你好」兩行,即使它應該是這樣的:
hello
hello information
重複兩次。這裏有什麼問題?
您是否將自定義單元格配置爲有足夠空間顯示兩個標籤? –
難道是'cell.info'被隱藏了嗎?你有沒有設置佈局約束?細胞的高度是多少? – Larme
@MuhammadHassan我將「行高」設置爲90,但它仍然顯示在我的手機上,非常窄,像35。這絕對是問題所在。儘管我的故事板看起來很寬。 – chibro2