2017-06-19 5 views
0

こんにちは、私はTableViewにオブジェクトの配列を表示したいが、配列の構成要素は1つだけ表示したい。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 
} 

}

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

問題は文字列変換とアンラップされたオプションにあると思います。これにより

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

はこれを交換してみてください

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

も正しい文字列を取得しながら、この置換は、安全に、オプションをアンラップ。

+0

はい!...この作品は完璧です! – xhinoda

+0

でも...配列をループする必要はありません。このコードだけが必要です:if str = autoCompleteDestino [index] .desDestino { cell.textLabel?.text = str } – xhinoda

+0

...ポストを少し簡略化して簡略化しました。 – bearacuda13

関連する問題