2016-11-18 42 views
0

我正在用swift为iPhone创建一个Todo应用程序。我想让tableview在tableview中显示任务的名称和时间。 Textlabel显示任务的正确名称,但detailTextLabel只显示Detail而不显示时间。swift ios中的tableview detailTextLabel

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! 
} 

回答

0

您对detailTextLabel的赋值是向后的。

if task.done { 

    cell?.textLabel!.text = "‼️\(task.name!)" 
    cell!.detailTextLabel!.text = task.time 
} 
else { 

    cell?.textLabel!.text = "\(task.name!)" 
    cell!.detailTextLabel!.text = task.time 

} 
+0

Thaks人。没有看到:D – tc34

+0

没问题!如果解决了它,请标记为答案。 (它做了什么;) – TheValyreanGroup

0

你从来没有真正将细节标签文本设置为任何东西。你需要的是这样设置的:

cell?.detailTextLabel!.text = task.time.longStyleDateString

及这里的日期延长:

extension Date { var longStyleDateString: String { let formatter = DateFormatter() formatter.dateStyle = .long return formatter.string(from: self) } }