I'm creating a Todo app with swift for iPhone. I want the tableview to show the name and the time of the task in a tableview. the Textlabel shows the correct name of the task but the detailTextLabel shows just Detail and not the time.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell") as UITableViewCell!
let task = tasks[indexPath.row]
if task.done {
cell?.textLabel!.text = "‼️\(task.name!)"
task.time = cell!.detailTextLabel!.text
} else {
cell?.textLabel!.text = "\(task.name!)"
task.time = cell!.detailTextLabel!.text
}
return cell!
}
Your assignment of the detailTextLabel is backwards.
if task.done {
cell?.textLabel!.text = "‼️\(task.name!)"
cell!.detailTextLabel!.text = task.time
}
else {
cell?.textLabel!.text = "\(task.name!)"
cell!.detailTextLabel!.text = task.time
}
You never actually set the detail label text to anything. You'll need to set it with something like this:
cell?.detailTextLabel!.text = task.time.longStyleDateString
And here's that Date extension:
extension Date { var longStyleDateString: String { let formatter = DateFormatter() formatter.dateStyle = .long return formatter.string(from: self) } }
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.