简体   繁体   中英

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
}

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM