59

detailTextLabel is not visible (code below). Can you tell me why?

 // Customize the appearance of table view cells.
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Configure the cell...

NSString *cellValue = [myListArray objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;

cell.detailTextLabel.text = @"Hello "; // This is not visible
cell.image = [myListArrayImages objectAtIndex:indexPath.row];

return cell;
}
xleon
  • 6,201
  • 3
  • 36
  • 52
Chatar Veer Suthar
  • 15,541
  • 26
  • 90
  • 154

6 Answers6

130

The detailTextLabel is not displayed for cells with the UITableViewCellStyleDefault style. init the UITableViewCell with UITableViewCellStyleSubtitle instead and you should see your detailTextLabel.

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
TheTiger
  • 13,264
  • 3
  • 57
  • 82
Opsimath
  • 1,997
  • 2
  • 18
  • 12
32

Or if you are using Interface Builder, change the Style cell property to Subtitle. :)

Style cell property in Interface Builder.

reinaldoluckman
  • 6,317
  • 8
  • 42
  • 50
2

In order to solve it programmatically:

let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "identifier")
Lukas Mohs
  • 290
  • 3
  • 4
1

I have used this and it worked for me:

// programming mark ----- ----- ---- ----- ----- ---- ----- ----- ----

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let CellIdentifier: String = "CellIdentifier"

    var cell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier) as? UITableViewCell

    if cell == nil {
        cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: CellIdentifier)
    }

    cell!.textLabel!.text = "Title"
    cell!.detailTextLabel!.text = "Value"

    return cell!
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Vinod Joshi
  • 7,696
  • 1
  • 50
  • 51
1

Swift 5

You can enable this inside cellForRowAt method

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell")

    cell.textLabel?.text = qus[indexPath.row]
    cell.detailTextLabel?.text = ans[indexPath.row]
    return cell
}
Umer Khan
  • 193
  • 12
  • May be a little introduction to what you have done differently might help others. – abhiarora May 06 '20 at 18:38
  • 1
    I am confused, why use dequeueReusableCell when you're redeclaring the cell on the next line? – Nathanael Jul 03 '20 at 17:33
  • 1
    This is NOT good, as you do not reuse the actual reusable cell. You create a new one. This can lead to performance issues. – vomi Mar 21 '22 at 14:18
0

I just want to mention that the wording in the UITableViewCell class reference can be a little bit confusing on this issue:

(After describing each cell type)

"Discussion In all these cell styles, the larger of the text labels is accessed via the textLabel property and the smaller via the detailTextLabel property."

It might seem that it's saying all of the cell types include a detailTextLabel, but if you carefully read through them it's only the default type that does not have a detailTextLabel.

DC2
  • 119
  • 8