【问题标题】:Reuse tablecell in static table view - iOS - Swift在静态表格视图中重用表格单元 - iOS - Swift
【发布时间】:2019-09-16 07:34:48
【问题描述】:

我是新手,学起来很快

我想在我的表格视图中使用静态单元格。我的所有单元格看起来都相似,所以我想将 xib 用于 tablecell 并在我的静态表格视图中使用它。

我目前面临的问题是,当我尝试访问我在 xib 中拥有的标签时,我在方法中抛出了错误:下面代码中的 ConfigureCell。 线程 1:致命错误:在隐式展开可选值时意外发现 nil 为什么我没有打开包装时说我是可选的

不知道这里需要做什么/请指教。

下面是我的代码:

class NewTableViewController: UITableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

       self.tableView.register(ReusableTableViewCell.self, forCellReuseIdentifier:"custom")
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 4
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "custom", for: indexPath) as? ReusableTableViewCell else {
            return UITableViewCell()
        }
        if indexPath.row == 0 {
            cell.configureCell(title: "Name", detail: "hello")
        }
        if indexPath.row == 2 {
            cell.configureCell(title: "Age", detail: "23")
        }
        if indexPath.row == 3 {
            cell.configureCell(title: "Dept", detail: "HR")
        }
        return cell
    }
}

class ReusableTableViewCell: UITableViewCell {

@IBOutlet weak var leftLabel: UILabel!
@IBOutlet weak var rightLabel: 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
}

func configureCell(title: String, detail: String) {

    leftLabel.text = title //Error Here: Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

    rightLabel.text = detail

}

}

【问题讨论】:

  • 能否添加错误内容以及哪行代码抛出的错误?
  • 刚刚添加。它在配置单元方法中
  • 我刚刚将您的 configureCell 函数添加到我自己的自定义单元格类中,该单元格类也有两个标签,并且还在 UITableViewController 中的 cellForRowAt 中调用它,它工作正常。我现在正在深入挖掘,但该功能以及您如何称呼它不是问题
  • 从 ReusableTableViewCell 类中,尝试从 awakeFromNib 中设置两个标签值,看看会发生什么。 rightLabel.text = "right label test" leftLabel.text = "left label test",请确保 cell.configureCell 函数调用在此测试的 tableViewController 中被注释掉,以便您仅在 ReusableTableViewCell 中设置标签类。
  • 实际上在 static 表格视图中所有单元格都没有被重用。它们是在表格视图中静态设计的。不使用数据源方法。由于单元格是静态的,您可以通过IBOutlets 访问 UI 元素

标签: ios swift uitableview reusability


【解决方案1】:

我找到了解决方案。问题出在这一行:

self.tableView.register(ReusableTableViewCell.self, forCellReuseIdentifier:"custom")

用下面的替换它解决了这个问题:

self.tableView.register(UINib(nibName: "ReusableTableViewCell", bundle: nil), forCellReuseIdentifier: "custom")

【讨论】:

    猜你喜欢
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多