[英]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?"
“无法将'Date'类型的值分配给字符串吗?”
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?"
“无法将'Date'类型的值分配给字符串吗?”
That is the first. 那是第一。 Exactly what the error says.
错误确切地说。
cell.textLabel?.text
expects a String
object and you are giving it a Date
object. cell.textLabel?.text
需要一个String
对象,而您给它一个Date
对象。 You need to convert the Date
to a String
, which can be done with the DateFormatter
class . 您需要将
Date
转换为String
,这可以使用DateFormatter
类完成 。
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. 您遇到的第二个问题是,如果要两个文本标签,您正在做的是为SAME标签设置两次文本。
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). 您将需要设置两个不同的标签,或者使用自定义单元格,或者使用Apple已预先配置为具有第二个UILabel的标签
cell.detailTextLabel
(我相信它们将其称为第二个标签cell.detailTextLabel
或类似的东西)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.