UITableViewCell not showing detailTextLabel.text - Swift

前端 未结 11 2247
慢半拍i
慢半拍i 2020-12-14 06:45

The detail (subtitle) text does not appear. The data are available, though, because when a println() call is added, it prints Optional(\"data\") to the console with the expe

11条回答
  •  囚心锁ツ
    2020-12-14 07:28

    Here is how it works for swift 5, to get a subtitle using detailtextlabel, using a UITableView object within a view controller, if you are missing any of these, it will not work and will probably crash.

    class ViewController: UIViewController, UITableViewDelegate,  UITableViewDataSource
    

    In viewDidLoad:

    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "subtitleCell")
    

    Delegate Function:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
        // Fetch a cell of the appropriate type.
        let cell = UITableViewCell(style: .subtitle , reuseIdentifier: "subtitleCell")
    
        // Configure the cell’s contents.
        cell.textLabel!.text = "Main Cell Text"
        cell.detailTextLabel?.text = "Detail Cell Text"
    
        return cell
    }
    

提交回复
热议问题