0

Hello can you help me I want to display in an TableView an array of objects but only one compoment of the array.

here my code:

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 want to show in this line, only the DestinoInstancia.desDestino = dep.desDestino!

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

Currently shows me this way:

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"))]enter image description here

When i want to show me only:

Asunción Miami Atenas Madrid

Please Help!

2
  • Looks like you are not unwrapping those optionals. Commented Jun 19, 2017 at 20:44
  • i think..yes, i unwrapping: DestinoInstancia.idDestino = dep.idDestino! DestinoInstancia.desDestino = dep.desDestino! Commented Jun 19, 2017 at 20:49

1 Answer 1

2

I think the issue lies in the String conversion and unwrapped optionals.

Try replacing this:

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

With this:

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

This replacement safely unwraps the optional while also retrieving the correct string.

Sign up to request clarification or add additional context in comments.

2 Comments

even...i don't need to loop the array...i need only this code: if let str = autoCompleteDestino[index].desDestino { cell.textLabel?.text = str }
Glad I could help... I simplified the post a bit to make it a little clearer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.