【问题标题】:How to show array of objects in TableView - swift如何在 TableView 中显示对象数组 - swift
【发布时间】:2017-06-19 20:41:30
【问题描述】:

你好,你能帮我吗?我想在 TableView 中显示一个对象数组,但只有一个数组的组成部分。

这里是我的代码:

extension ViewController: UITableViewDataSource {
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
    let index = indexPath.row as Int

    for dep in autoCompleteDestino {
        DestinoInstancia.idDestino = dep.idDestino!
        DestinoInstancia.desDestino = dep.desDestino!
        autoCompleteDestino.append(dep)
    }

    print(autoCompleteDestino)
    cell.textLabel?.text = String(describing: autoCompleteDestino[index])

    return cell
}

}

所以..我想在这一行显示,只有 DestinoInstancia.desDestino = dep.desDestino!

cell.textLabel?.text = String(describing: autoCompleteDestino[index])

目前以这种方式显示:

MTiOS.Destinos(idDestino: Optional(1), desDestino: Optional("Asunción")), MTiOS.Destinos(idDestino: Optional(2), desDestino: Optional("Miami")), MTiOS.Destinos(idDestino : Optional(3), desDestino: Optional("Atenas")), MTiOS.Destinos(idDestino: Optional(5), desDestino: Optional("Madrid"))]

当我只想给我看时:

亚松森 迈阿密 阿泰纳斯 马德里

请帮忙!

【问题讨论】:

  • 看起来你没有解开这些选项。
  • 我想..是的,我打开包装:DestinoInstancia.idDestino = dep.idDestino! DestinoInstancia.desDestino = dep.desDestino!

标签: arrays swift uitableview tableviewcell


【解决方案1】:

我认为问题在于字符串转换和未包装的选项。

尝试替换这个:

    cell.textLabel?.text = String(describing: autoCompleteDestino[index])

有了这个:

    if let str = autoCompleteDestino[index].desDestino {
        cell.textLabel?.text = str
    }

这个替换可以安全地解开可选项,同时还能检索到正确的字符串。

【讨论】:

  • 甚至...我不需要循环数组...我只需要以下代码: if let str = autoCompleteDestino[index].desDestino { cell.textLabel?.text = str }
  • 很高兴我能帮上忙...我简化了帖子以使其更清晰。
猜你喜欢
  • 1970-01-01
  • 2015-05-14
  • 1970-01-01
  • 2018-11-12
  • 1970-01-01
  • 2017-11-24
  • 1970-01-01
  • 2020-11-02
  • 1970-01-01
相关资源
最近更新 更多