【发布时间】:2017-11-08 12:45:44
【问题描述】:
我在 ViewController 上有一个 UITableView。这是完整的代码
import UIKit
class UnitTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var unitTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.unitTable.register(UnitCell.self, forCellReuseIdentifier: "UnitCell")
self.unitTable.delegate = self
self.unitTable.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = unitTable.dequeueReusableCell(withIdentifier: "UnitCell", for: indexPath) as! UnitCell
cell.unitNameLbl?.text = "TEST"
return cell
}
}
但不幸的是,我的自定义单元格不可见。我已经设置了绿色背景颜色以确保约束是正确的。 这是我的 UnitCell 代码
import UIKit
class UnitCell: UITableViewCell {
@IBOutlet weak var unitNameLbl: UILabel?
@IBOutlet weak var unitNumberLbl: UILabel?
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
}
}
如果我看不到自定义单元格,会出现什么问题?
【问题讨论】:
-
您是否设置了估算高度和/或行高?否则我相信它默认为 44pts。
-
检查cell的reuseIdentifier。如果给出正确与否。并尝试 unitTable.register(UINib(nibName: "yourIdentifier", bundle: nil), forCellReuseIdentifier: "yourIdentifier") 。注册一个笔尖
-
也许你已经将它作为一个类而不是 Nib 出列。正如 Arpita 提到的,尝试将其注册为 Nib
-
@arpita 没错!我刚刚放了 unitTable.register(UINib(nibName: "UnitCell", bundle: nil), forCellReuseIdentifier: "UnitCell") 并按预期工作。并且还删除了 self.unitTable.register(UnitCell.self, forCellReuseIdentifier: "UnitCell")
-
没错@Micgal
标签: ios iphone swift uitableview