[英]Table view showing empty
我基本上想創建一個自定義視圖並在其中添加一個表格視圖。 我能夠成功地做到這一點,並且在我運行代碼時表格視圖也可以正確顯示。 但是,盡管在viewDidLoad
中注冊了自定義 tablview 單元格,但 tablview 顯示為空。 它是一個自定義的 tableview 單元格,我設計為加載到 tablview 中。
我做的自定義視圖class的.swift文件看起來像這樣...
import UIKit
@IBDesignable class TagResolutionView: UIView {
@IBOutlet var tagResolutionView: UIView!
@IBOutlet private weak var tableview: UITableView!
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
override func awakeFromNib() {
super.awakeFromNib()
}
@IBOutlet weak var delegate: UITableViewDelegate? {
get {
return tableview.delegate
}
set {
tableview.delegate = newValue
}
}
@IBOutlet weak var dataSource: UITableViewDataSource? {
get {
return tableview.dataSource
}
set {
tableview.dataSource = newValue
}
}
func registerClass(cellClass: AnyClass?, forCellReuseIdentifier identifier: String) {
tableview.register(cellClass, forCellReuseIdentifier: "cellClass")
}
func dequeueReusableCellWithIdentifier(identifier: String) -> UITableViewCell? {
return tableview.dequeueReusableCell(withIdentifier: identifier)
}
private func commonInit() {
Bundle.main.loadNibNamed("TagResolutionView", owner: self, options: nil)
addSubview(tagResolutionView)
tagResolutionView.frame = self.bounds
tagResolutionView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}
}
這就是我在主視圖控制器中設置表格視圖的方式......
在viewDidLoad
中,
standardsProceduresView.delegate = self
standardsProceduresView.dataSource = self
standardsProceduresView.registerClass(cellClass: UpdatingListTableViewCell.self, forCellReuseIdentifier: "cellClass")
並進一步,
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UpdatingListTableViewCell = self.standardsProceduresView.dequeueReusableCellWithIdentifier(identifier: "cellClass") as! UpdatingListTableViewCell
cell.nameLbl.text = "MyName" //But here I get a crash saying 'Unexpectedly found nil while implicitly unwrapping an Optional value'
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
該崩潰告訴您單元的出隊或將該單元的強制轉換為給定的 class 存在問題。 也許一個接一個地做這些並添加一個斷點(或打印)以查看在強制轉換之前出隊的結果是什么:
let cell = self.standardsProceduresView.dequeueReusableCellWithIdentifier(identifier: "cellClass")
print(cell)
let updatingCell = cell as! UpdatingListTableViewCell
我還可以看到您在注冊class之前設置了dataSource
,這可能會導致問題。 嘗試在設置dataSource
之前先注冊 class 。
問題是我沒有注冊筆尖...
而不是這個..
func registerClass(cellClass: AnyClass?, forCellReuseIdentifier identifier: String) {
tableview.register(cellClass, forCellReuseIdentifier: "cellClass")
}
我不得不寫這個...
func registerClass(cellClass: AnyClass?, forCellReuseIdentifier identifier: String) {
let nib = UINib(nibName: "UpdatingListTableViewCell", bundle: nil)
tableview.register(nib, forCellReuseIdentifier: "cellClass")
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.