Why is detailTextLabel not visible?

Objective CUitableviewCellDetailtextlabel

Objective C Problem Overview


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;
}

Objective C Solutions


Solution 1 - Objective C

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];

Solution 2 - Objective C

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

Style cell property in Interface Builder.

Solution 3 - Objective C

In order to solve it programmatically:

let cell = UITableViewCell(style: .subtitle, reuseIdentifier: "identifier")

Solution 4 - Objective C

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
}

Solution 5 - Objective C

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!
}

Solution 6 - Objective C

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChatar Veer SutharView Question on Stackoverflow
Solution 1 - Objective COpsimathView Answer on Stackoverflow
Solution 2 - Objective CreinaldoluckmanView Answer on Stackoverflow
Solution 3 - Objective CLukas MohsView Answer on Stackoverflow
Solution 4 - Objective CUmer KhanView Answer on Stackoverflow
Solution 5 - Objective CVinod JoshiView Answer on Stackoverflow
Solution 6 - Objective CDC2View Answer on Stackoverflow