我为表格 View 创建了一个扩展以方便注册单元格:
extension UITableView {
func mapTableViewWithCellsDictionary(dictionary : [String : String]) -> Void {
for (key, _) in dictionary {
self.register(NSClassFromString(dictionary[key]!), forCellReuseIdentifier: key)
}
}
}
在我的 View Controller 中我做了:
let dct = ["VacanciesItem":"VacanciesCell"]
tableView = UITableView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
tableView.separatorStyle = .none
tableView.mapTableViewWithCellsDictionary(dictionary: dct)
tableView.dataSource = self
tableView.delegate = self
tableView.estimatedRowHeight = 80
tableView.rowHeight = UITableViewAutomaticDimension
self.view.addSubview(tableView)
然后:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : ModelBinding = tableView.dequeueReusableCell(withIdentifier: "VacanciesItem", for: indexPath as IndexPath) as! ModelBinding
cell.bindWithModel(model: self.viewModel.arrValues[indexPath.row])
return cell as! UITableViewCell
}
但是,应用程序无法看到该单元格,它会抛出一个错误:
'unable to dequeue a cell with identifier VacanciesItem - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
但是我已经注册过了。
最佳答案
错误在这里
for (key, _) in dictionary {
self.register(NSClassFromString(dictionary[key]!), forCellReuseIdentifier: key)
}
您正在使用 VacanciesItem
作为类和重用标识符。我假设你的意思是
for (key, value) in dictionary {
self.register(NSClassFromString(dictionary[value]!), forCellReuseIdentifier: key)
}
这样您就可以使用 VacanciesItem
标识符注册 VacanciesCell
类
关于ios - Swift 无法使单元格出队,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45280416/