【问题标题】:Xib file UITableViewCell outlets are nilXib 文件 UITableViewCell 出口为零
【发布时间】:2021-03-23 15:10:04
【问题描述】:

这是一个持续的话题,但我的情况略有不同。我正在使用this tutorial。我有一个视图控制器,它有自己的故事板,这个视图控制器有表格视图。视图控制器是该表的委托和数据源。我需要向该表中添加不同类型的单元格,并且为此我也使用了单元格视图模型。

单元格:

class TitleTextCell: UITableViewCell, CellConfigurable {
        
        @IBOutlet weak var titleLabel: UILabel!
        @IBOutlet weak var textView: UITextView!
        static let identifier = "TitleTextCell"
        
        
        func setup(viewModel: RowViewModel) {
            guard let titleTextViewModel = viewModel as? TitleTextViewModel else {return}

            titleLabel.text = titleTextViewModel.title //fatal: found nil while 
            textView.text = titleTextViewModel.text    //unwrapping an optional value
        }
    }

表格视图控制器的 ViewController:

class InfoViewController: UIViewController, Storyboarded {
        // MARK: - Properties
        var viewModel: InfoViewModelType! {
            didSet {
                viewModel.viewDelegate = self
            }
        }
        
        // MARK: - Outlets
        @IBOutlet weak var tableView: UITableView!
        
        // MARK: - Lifecycle Methods
        override func viewDidLoad() {
            super.viewDidLoad()
            tableView.register(TitleTextCell.self, forCellReuseIdentifier: TitleTextCell.identifier)
            tableView.delegate = self
            tableView.dataSource = self  
        }
    }

    extension InfoViewController: UITableViewDelegate, UITableViewDataSource {
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return viewModel.tableRows.count
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let rowViewModel = viewModel.tableRows[indexPath.row]
            let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier(for: rowViewModel), for: indexPath)
            if let cell = cell as? CellConfigurable {
                cell.setup(viewModel: rowViewModel)
            }
            return cell
        }
        
        private func cellIdentifier(for viewModel: RowViewModel) -> String {
            switch viewModel {
            case is TitleTextViewModel:
                return TitleTextCell.identifier
            default:
                fatalError("Unexpected view model type: \(viewModel)")
            }
        }
    }

单元格是一个 xib 文件。出口与文件所有者相连(见屏幕)。

它确实到达了func setup(viewModel: RowViewModel) 的位置,这意味着从表格来看它是正确的。但是在运行时出口为零,我错过了什么?

【问题讨论】:

  • 如果单元格在 xib 中,则tableView.register(TitleTextCell.self, forCellReuseIdentifier: TitleTextCell.identifier) 不是注册单元格的正确方法。它正在注册该类,但并未将其“附加”到 xib。相反,使用register(_ nib: UINib?, forCellReuseIdentifier identifier: String)

标签: ios swift uitableview


【解决方案1】:

你需要

tableView.register(UINib(nibName: "TitleTextCell", bundle: nil), forCellReuseIdentifier: TitleTextCell.identifier)

对于 xib 单元格

【讨论】:

  • 这与一条评论一起工作:我需要断开文件所有者的连接,从文件所有者中删除 TitleTextCell,并将其添加到 xib 的 UITableViewCell 的类中,然后重新连接插座然后直接连接到此。谢谢。
猜你喜欢
  • 2020-04-04
  • 2018-12-10
  • 2017-06-14
  • 2019-06-04
  • 2012-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-24
相关资源
最近更新 更多