2017-06-19 29 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