表视图显示空

问题描述 投票:0回答:2

我基本上是想创建一个自定义视图,并在其中添加一个tableview。我能够成功地做到这一点,而且当我运行代码时,tableview也能正常显示。但是tablview显示为空,尽管我已经在 viewDidLoad. 它是一个自定义的tableview单元格,我已经设计好在tablview中加载。

我制作的自定义视图类的.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]

    }
}

这是我在主视图控制器中设置tableview的方法......

在我的主视图控制器中,我是这样设置tableview的... 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
}
ios swift tableview
2个回答
0
投票

这种崩溃告诉你,单元格的dequeueing或单元格投向给定类的过程中存在一些问题。也许可以一个接一个地做,并添加一个断点(或打印),看看在投递之前的dequeue的结果是什么。

let cell = self.standardsProceduresView.dequeueReusableCellWithIdentifier(identifier: "cellClass")
print(cell)
let updatingCell = cell as! UpdatingListTableViewCell

我还可以看到你在单元格中设置了 dataSource 在您注册班级之前,请先注册班级,这可能会导致问题。尽量先注册班级,然后再设置。dataSource.


0
投票

问题是我没有注册笔尖... ...

而不是这样...

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")
    }
© www.soinside.com 2019 - 2024. All rights reserved.