【问题标题】:tableview in swift ios detailTextLabelswift ios中的tableview detailTextLabel
【发布时间】:2016-11-18 19:08:22
【问题描述】:

我正在为 iPhone 创建一个带有 swift 的 Todo 应用程序。我希望 tableview 在 tableview 中显示任务的名称和时间。 Textlabel 显示任务的正确名称,但 detailTextLabel 仅显示 Detail 而不是时间。

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

【问题讨论】:

    标签: ios iphone swift uitableview tableview


    【解决方案1】:

    您对 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
    
    }
    

    【讨论】:

    • 没问题!如果解决了,请标记为答案。 (它做到了;)
    【解决方案2】:

    您实际上从未将详细标签文本设置为任何内容。你需要像这样设置它:

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

    这是日期扩展:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 2020-04-24
      • 2020-11-28
      • 2014-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多