UITableViewCell not showing detailTextLabel.text - Swift

前端 未结 11 2350
慢半拍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:18

    If doing so programmatically without cells in interface builder this code works like a charm in Swift 2.0+

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    
        yourTableView.delegate = self
        yourTableView.dataSource = self
        yourTableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "subtitleCell")
    
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return yourTableViewArray.count
    }
    
    func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
        let cell: UITableViewCell = yourTableView.dequeueReusableCellWithIdentifier("subtitleCell", forIndexPath: indexPath) as UITableViewCell
    
        cell.textLabel?.text = "the text you want on main title"
        cell.detailTextLabel?.text = "the text you want on subtitle"
    
        return cell
    }
    

提交回复
热议问题