49

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.

djromero
  • 19,551
  • 4
  • 71
  • 68
RobertL
  • 14,214
  • 11
  • 38
  • 44

9 Answers9

123

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.

Aurum Aquila
  • 9,126
  • 4
  • 25
  • 24
  • Right! Thank you. I had tried to change the style to Subtitle but I changed UITableViewStyle instead of UITableViewCellStyle and that didn't compile so I thought the Subtitle style was no longer available. – RobertL Jan 29 '11 at 00:18
  • 1
    Haha! We all make silly mistakes when coding. I think if Apple disabled the subtitle style, there would be riots. – Aurum Aquila Jan 29 '11 at 00:19
  • 5
    This seems like it will initialized a new UITableViewCell every time, rather than dequeuing from the stack to preserve memory. It seems like the only way to reuse cells of style UITableViewCellStyleSubtitle would be to subclass, override the initializer, and call the usual `[tableView dequeueReusableCellWithIdentifier:@"ReuseIdentifier"];` Can anyone confirm or deny this? See discussion on `initWithStyle:reuseIdentifier:` https://developer.apple.com/library/ios/documentation/Uikit/reference/UITableViewCell_Class/index.html#//apple_ref/occ/instm/UITableViewCell/initWithStyle:reuseIdentifier: – Giles Van Gruisen Oct 09 '14 at 22:22
  • 1
    Just in case anyone wants the Swift 4 version of @AurumAquila's answer: `let cell = UITableViewCell(style: UITableViewCell.CellStyle.subtitle, reuseIdentifier: "cell")` – agrippa Jul 27 '19 at 22:24
  • Thanks @agrippa. Do we still need this line of code: `let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)`? – timman Apr 04 '20 at 17:52
15

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

if (!cell) {
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:BasicCellIdentifier];
}
W Dyson
  • 4,604
  • 4
  • 40
  • 68
14

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

oragorn
  • 181
  • 1
  • 7
4
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
                           reuseIdentifier:CellIdentifier];
kleopatra
  • 51,061
  • 28
  • 99
  • 211
4

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

Mohammad Shaker
  • 185
  • 1
  • 15
3

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
balazs630
  • 3,421
  • 29
  • 46
3

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.

SomaMan
  • 4,127
  • 1
  • 34
  • 45
  • 1
    I wish I could give you +100 What a simple answer. Lots of complicated answeres around this. But this answer is obviously the way to go for swift4 ! Thank you !!!! – Julian Silvestri Oct 01 '18 at 15:40
  • 5
    How do you register a cell of type `subtitle` programmatically without using a storyboard? – Justin Vallely Jan 31 '19 at 05:29
  • 1
    For anybody still wondering. Just subclass a UITableViewCell and in the init declaration call super forcing the style to be subtitle. Like so: override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: .subtitle, reuseIdentifier: reuseIdentifier) } Then register your custom cell – Gautier Mar 27 '21 at 08:53
0
  cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier: CellIdentifier];
0

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

l-spark
  • 871
  • 2
  • 10
  • 25