【问题标题】:Cannot assign value of type 'String' to type 'MyOrderTableViewCell'无法将“String”类型的值分配给“MyOrderTableViewCell”类型
【发布时间】:2017-11-02 04:06:50
【问题描述】:

我想点击基于ID的行,

所以我声明了一个数组来存储 ID:

var idenArr = [String]()  

在 didSelectRowAt 索引路径上:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        var cell = tableView.dequeueReusableCell(withIdentifier: "myOrderTableViewCell", for: indexPath as IndexPath) as! MyOrderTableViewCell
        cell = idenArr[indexPath.row] `<-- Here show me this error "Cannot assign value of type 'String' to type 'MyOrderTableViewCell'"`
}

【问题讨论】:

  • ||请阅读有关表格视图的信息。以上 2 行中还有很多其他问题,而不仅仅是您指出的问题。为什么您需要在 didSelectRowAt 中对一个单元格进行双端队列,这是 cellForRowAt 的工作?您正在分配字符串值以键入 MyOrderTableViewCelll。
  • 在错误行中,您的代码实际上是在尝试将 id 的值分配给 Cell。这就是它来的原因。
  • cell =更改为cell.textLabel.text =
  • 永远不要在cellForRowAt之外打电话给tableView.dequeueReusableCell

标签: ios swift didselectrowatindexpath


【解决方案1】:

要获得这个,您可以通过以下方式获得它:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let ID = idenArr[indexPath.row]
    print("Your selected Id is:" ID)
}

【讨论】:

    猜你喜欢
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    相关资源
    最近更新 更多