RobertL
RobertL

Reputation: 14884

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

In tableView:cellForRowAtIndexPath: I have the following:

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

The textLabel shows as expected, but the detailTextLabel doesn't appear at all, although there is no diagnostic. What I expected was that the "detail" text would appear in the cell on a second line, below the "normal" text, possibly with a smaller font size.

The same question is asked in another posting here and user "jbrennan" answered that the tableview cell style must be something other than UITableViewCellStylePlain. However, it seems there are only two possible styles, UITableViewCellStylePlain and UITableViewCellStyleGrouped. I get the same result with either (the detail label doesn't appear).

Is there another cell style that I'm not seeing in the documentation? Did UITableView change in the last update and detailTextLabel is no longer available? Do I have to do something extra to make it appear? Any suggestions?

I'm using xcode 3.2.5 and building for iPhone 4.2 Simulator.

Upvotes: 49

Views: 62925

Answers (9)

l-spark
l-spark

Reputation: 881

Make sure to set the default text content, and the width is width is wide enough to actually display the text.

Upvotes: 0

user5018827
user5018827

Reputation:

  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier: CellIdentifier];

Upvotes: 0

SomaMan
SomaMan

Reputation: 4164

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.

Upvotes: 3

balazs630
balazs630

Reputation: 3691

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
}

enter image description here

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

Upvotes: 3

W Dyson
W Dyson

Reputation: 4634

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

if (!cell) {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:BasicCellIdentifier];
}

Upvotes: 15

Mohammad Shaker
Mohammad Shaker

Reputation: 185

Set the table View Cell Style to Subtitle in Storyboard

and write this code to configure you cell

if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:BasicCellIdentifier] autorelease];
}

enter image description here

Upvotes: 4

user2964955
user2964955

Reputation: 51

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

Upvotes: 4

oragorn
oragorn

Reputation: 181

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

Upvotes: 14

Aurum Aquila
Aurum Aquila

Reputation: 9126

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.

Upvotes: 123

Related Questions