ios - Swift 无法使单元格出队

标签 ios swift uitableview

我为表格 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/

相关文章:

ios - 以圆形模式向外移动 SpriteNode

ios - 如何快速更改 Controller View 某些部分的颜色?

iphone - UIDevice 模型与 localizedModel

ios - 如何使 UITextField 的行为类似于 swift 3 中的银行输入格式?

ios - 字母滚动条不显示

ios - 将 UITableViewCell 中的 UIButton 转换为自定义类不起作用

ios - 当用户点击带有 `UITableView` 的单元格时,在 `UITextField` 上添加自动完成列表/表格

ios - 雪碧包 : how to make newly spawned enemy ships shoot bullets at player?

ios - Swift:如何将短音播放得更长

swift - 枚举函数并在 swift 上用新值减去旧值