Arrays 如何在TableView中显示对象数组-swift

Arrays 如何在TableView中显示对象数组-swift,arrays,swift,uitableview,tableviewcell,Arrays,Swift,Uitableview,Tableviewcell,您好,您能帮我吗?我想在TableView中显示一个对象数组,但仅显示数组的一个组成部分 这是我的代码: extension ViewController: UITableViewDataSource { public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusable

您好,您能帮我吗?我想在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
}
}

所以..我想在这行中显示,只有destinoinstancea.destino=dep.destino

cell.textLabel?.text = String(describing: autoCompleteDestino[index])
目前我的看法是:

MTiOS.Destinos(idDestino:Optional(1)、destino:Optional(“亚松森”)、MTiOS.Destinos(idDestino:Optional(2)、destino:Optional(“迈阿密”)、MTiOS.Destinos(idDestino:Optional(3)、destino:Optional(“Atenas”)、MTiOS.Destinos(idDestino:Optional(5)、destino:Optional(“马德里”)]

当我只想给我看:

亚松森 迈阿密 阿特纳斯队 马德里


请帮忙

我认为问题在于字符串转换和展开选项

尝试替换此:

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

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

此替换操作在检索正确字符串的同时安全地展开可选项。

我认为问题在于字符串转换和展开选项

尝试替换此:

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

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

此替换操作在检索正确字符串的同时安全地展开可选字符串。

看起来您没有展开这些可选字符串。我想..是的,我正在展开:destinoinstantia.idDestino=dep.idDestino!destinoinstancea.destino=dep.destino!看起来你没有打开那些选项。我想..是的,我正在打开:destinoinstancea.idDestino=dep.idDestino!destinoinstancea.destino=dep.destino!甚至…我不需要循环数组…我只需要以下代码:if let str=autoCompleteDestino[index].desDestino{cell.textlab?.text=str}很高兴我能帮上忙。。。我简化了这篇文章,使它更清晰。甚至…我不需要循环数组…我只需要以下代码:if let str=autoCompleteDestino[index].desDestino{cell.textlab?.text=str}很高兴我能帮上忙。。。我把帖子简化了一点,让它更清楚一点。