UITableViewCell not showing detailTextLabel.text - Swift

前端 未结 11 2250
慢半拍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条回答
  • Some of the solutions above are not entirely correct. Since the cell should be reused, not re-created. You can change init method.

    final class CustomViewCell: UITableViewCell {
        override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
            super.init(style: .value1, reuseIdentifier: reuseIdentifier)
        }
    
        required init?(coder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    
    0 讨论(0)
  • 2020-12-14 07:25

    For what it's worth: I had the problem of detail not appearing. That was because I had registered the tableView cell, which I should not have done as the cell prototype was defined directly in storyboard.

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2020-12-14 07:29

    In Xcode11 and Swift5 , We have to do like below. If we do it by checking the condition cell == nil and then creating UITableViewCell with cellStyle it is not working . Below solution is working for me .

        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
               let cellIdentifier = "Cell"
                let cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: cellIdentifier)
        cell?.textLabel?.text = "Title"
        cell?.detailTextLabel?.text = "Sub-Title"
    
       return cell!
        }
    
    0 讨论(0)
  • 2020-12-14 07:31

    Same issue here (from what I've read, perhaps a bug in iOS 8?), this is how we worked around it:

    1. Delete the prototype cell from your storyboard

    2. Remove this line:

    var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

    1. Replace with these lines of code:
    let cellIdentifier = "Cell"
    
    var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as? UITableViewCell
    if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.Value2, reuseIdentifier: cellIdentifier)
    }
    

    Update for Swift 3.1

    let cellIdentifier = "Cell"
    
    var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
    if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.value2, reuseIdentifier: cellIdentifier)
    }
    

    Update for Swift 4.2 - Simplified

    let cell = UITableViewCell(style: UITableViewCell.CellStyle.value2, reuseIdentifier: "cellId")
    
    0 讨论(0)
提交回复
热议问题