2017-06-19 105 views
0

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

这里我的代码:

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 
} 

}

So..i想在这一行中,只有DestinoInstancia.desDestino = dep.desDestino展现!

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

目前显示我这样说:

MTiOS.Destinos(idDestino:可选(1),desDestino:可选( “亚松森”)),MTiOS.Destinos(idDestino:选购(2) ,可选(“迈阿密”)),MTiOS.Destinos(idDestino:可选(3),desDestino:可选(“Atenas”)),MTiOS.Destinos(idDestino:可选(5),desDestino:可选(“Madrid” ))] enter image description here

当我只想告诉我:

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

请帮助!

+0

看起来你不打开这些可选项。 – ffritz

+0

我认为..,我解开:DestinoInstancia.idDestino = dep.idDestino! DestinoInstancia.desDestino = dep.desDestino! – xhinoda

回答

1

我认为问题在于字符串转换和unwrapped optionals。

尝试更换此:

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

有了这个:

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

这种替换安全解开可选,同时还获取正确的字符串。

+0

是的!......这项工作非常完美! – xhinoda

+0

甚至...我不需要循环阵列...我只需要这样的代码:if let str = autoCompleteDestino [index] .desDestino { cell.textLabel?.text = str } – xhinoda

+0

很高兴我能帮到你...我简化了这篇文章,使其更清晰一些。 – bearacuda13

相关问题