In UITableView, cell.detailTextLabel.text isn't working… why?

前端 未结 9 1019
逝去的感伤
逝去的感伤 2020-12-08 00:34

In tableView:cellForRowAtIndexPath: I have the following:

cell.textLabel.text = @\"label\";
cell.detailTextLabel.text = @\"detail\";


        
相关标签:
9条回答
  • 2020-12-08 00:48

    Swift 3:

    If you're making your cell with overriding the "cellForRowAt indexPath" function:

    let tableContent = ["1", "2", "3", "4", "5"]
    let tableDetailContent = ["a", "b", "c", "d", ""]
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "yourCellIdentifier")
        cell.textLabel?.text = tableContent[indexPath.row]
        cell.detailTextLabel?.text = tableDetailContent[indexPath.row]
    
        return cell
    }
    

    This code part from above allows you to set your detailTextLabel.. You can set whatever you want on storyboard for Table View Cell style (Custom, Basic, Right Detail, Left Detail, Subtitle), this line will override it:

    style: UITableViewCellStyle.subtitle
    
    0 讨论(0)
  • 2020-12-08 00:49

    When using Xcode 4.2, set the Table View Cell style to Subtitle in Storyboard. dequeueReusableCellWithIdentifier will return an instantiated cell.

    0 讨论(0)
  • 2020-12-08 00:52

    Your initialization needs to be changed to this:

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
    reuseIdentifier:CellIdentifier] autorelease];

    I've emphasized and bolded the part you need to change.

    0 讨论(0)
  • 2020-12-08 00:53

    You need to set the cell type to Subtitle when you allocate it.

    if (!cell) {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:BasicCellIdentifier];
    }
    
    0 讨论(0)
  • 2020-12-08 00:53
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                               reuseIdentifier:CellIdentifier];
    
    0 讨论(0)
  • 2020-12-08 00:53

    For Swift 4, though the technique would be the same whatever version (or Obj-C) -

    I know it's a bit late to the party, but a lot of answers are making this much more complicated than it needs to be.

    Assuming you're using a storyboard, all you need to do is set the Table View Cell Style in the storyboard to 'Subtitle', then just -

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "myIdentifier", for: indexPath)
    
        cell.textLabel?.text = "Title"
        cell.detailTextLabel?.text = "Subtitle..."
    
        return cell
    }
    

    There is absolutely no need to use code to instantiate a cell, just reuse as usual.

    0 讨论(0)
提交回复
热议问题