user7748962
user7748962

Reputation:

Two labels for UITableViewCell

I am trying to get a date value to display below the name value on the table view but I keep getting error messages. "Cannot assign the value of type 'Date' to type string?"

  func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = UITableViewCell()

    let ots = OTSs[indexPath.row]

    cell.textLabel?.text = ots.branch!
    cell.textLabel?.text = ots.enterDate!



    return cell
}

Upvotes: 0

Views: 897

Answers (1)

Jake G
Jake G

Reputation: 1195

You've got several issues.

"Cannot assign the value of type 'Date' to type string?"

That is the first. Exactly what the error says. cell.textLabel?.text expects a String object and you are giving it a Date object. You need to convert the Date to a String, which can be done with the DateFormatter class.

The second issue you have, is if you want two text labels, what you are doing is setting the text twice for the SAME label.

Instead of:

cell.textLabel?.text = ots.branch! cell.textLabel?.text = ots.enterDate!

you would need to set two different labels, either using a custom cell or one of the ones Apple has preconfigured to have a 2nd UILabel (I believe they call the second label cell.detailTextLabel or something along those lines).

Upvotes: 1

Related Questions