UITableViewCell not showing detailTextLabel.text - Swift

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

    xcode 11

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //        let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier", for: indexPath)
        let cell = UITableViewCell(style: UITableViewCell.CellStyle.value1, reuseIdentifier: "reuseIdentifier")
        cell.detailTextLabel?.text = "Detail text"
        cell.textLabel?.text = "Label text"
    
        // Configure the cell...
    
        return cell
    }
    
    0 讨论(0)
  • 2020-12-14 07:09

    Try this it work for me (swift 5)

    let cell = UITableViewCell(style: .value1, reuseIdentifier: "cellId")
    cell.textLabel.text = "Déconnexion"
    cell.imageView.image = UIImage(named: "imageName")
    

    Objective c :

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cellId"];
    
    0 讨论(0)
  • 2020-12-14 07:11

    Your code looks fine. Just goto the storyboard and select the cell of your tableview -> Now goto Attributes Inspector and choose the style to Subtitle.

    Follow this according to the below screenshot.

    Hope it helped..

    0 讨论(0)
  • 2020-12-14 07:14

    If you still want to use prototype cell from your storyboard, select the TableViewcell style as Subtitle. it will work.

    0 讨论(0)
  • 2020-12-14 07:17

    If you are setting the text to nil somewhere when you try to set it to a non-nil value the actual view that contains the text will be missing. This was introduced in iOS8. Try setting to an empty space @" " character instead.

    See this: Subtitles of UITableViewCell won't update

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
提交回复
热议问题