Why is my UITableViewCell not showing detailTextLabel in ANY style?

I have hair pulling. I'm just trying to create a table with a UITableViewCellStyleSubtitle style with text and details. But the details do not appear. This usually happens because someone forgot to initialize the cells with the correct style, but this is not what happens in this case. I tried all four cell styles and the details are not displayed in any of them, although the text label moves to the right when I use UITableViewCellStyleValue2.

I have successfully created tables many times before. The only significant difference I can come up with in this case is that I do not use the UITableViewController. Instead, I embed a UITableView, like any other view, and connect my own data source and delegate.

I applied the key method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // Get the UITableViewCell (out of the recycling pool, or de novo) NSString *reuseID = @"CELL"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID]; if (not cell) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:reuseID] autorelease]; } cell.textLabel.text = @"Foo"; cell.detailTextLabel.text = @"Bar"; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.detailTextLabel.frame = CGRectMake(10, 20, 200,22); // required cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:12]; // also required //[cell setNeedsLayout]; (makes no difference) return cell; } 

I can see the text text only if I include the BOTTOM of the “required” lines above (and use any cell style other than Default), and even when I do this, the position of the text label completely covers the position of the part label.

So it looks like the initializer creates the detailTextLabel UILabel, but sets its font size to zero and its frame to something empty or off-screen. (I tried to check them in the debugger, but this is not very useful - the font size is zero, and the frame is empty for BOTH textLabel and detailTextLabel, but textLabel is always displayed normally.)

Obviously, I can get around this if necessary by manually adjusting the sizes and fonts. But it bothers me that I need to do this in this case, when usually you just set the style, and the text and layout are automatic. I searched googles and, but cannot find a link to a similar problem. Does anyone know what is going on here?

(Testing for iOS 6.1.)

+4
source share
5 answers

After using all of these things, when I set the table cell style to “Subtitle” in the Storyboard, a subtitle appeared for me IB Storyboard Cell setting

+16
source

Registering the standard UITableViewCell class prevents the use of a stylized UITableViewCells because this code block will never be executed

 if (not cell) { // .... Cell will never be nil if a class/nib is registered } 

Solution . Are you registering a nib or class in your TableView? If you have a line like the following, you should delete it

 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kMCControlsCellIdentifier]; 

and update the dequeue statement as follows

 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kMCControlsCellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kMCControlsCellIdentifier]; } 
+5
source

OK, it turned out that in this application we have a very unusual build process that incorrectly identified the SDK, the application was created against Cocoa.

As a result, Cocoa thought it was a very old application - from days to iOS4, before the TextLabel part was added. Thus, the structure tried to be useful by emulating the behavior of pre-iOS4 (hiding the detailTextLabel in several ways). Setting a breakpoint on _UIApplicationLinkedOnOrAfter and forcing it to return 1 allowed me to hack this function and prove that this was the reason. (And now we just need to fix our build process to report the correct version of the SDK.)

Needless to say, this is not what most people are about to face. But I thought that I would send an answer here for the sake of posterity.

+3
source

Hope this helps you someday check this out.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; } cell.textLabel.text = @"Foo"; cell.detailTextLabel.text = @"Bar"; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; cell.textLabel.frame = CGRectMake(10, 20, 100,22); return cell; } 

operations such as font size, background color change for cell.labels, all these functions that you must perform in this method described below.

 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:10]; cell.textLabel.backgroundColor=[UIColor redColor]; cell.textLabel.frame = CGRectMake(10, 20, 100,22); cell.detailTextLabel.frame = CGRectMake(10, 20, 200,22); } 

if you change this type of property in cellForRowAtIndexPath using this method, you will lose these properties.

+1
source

The problem is that you most likely registered your cell using

 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"]; 

Value

 if (not cell) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:reuseID] autorelease]; } 

will never be called because the cell will never be zero. Therefore, the cell will always be initialized in the UITableViewCellStyleNone table.

The only way to fix this is to register a cell from the storyboard or subclass UITableViewCell and override

 (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 

method.

This works for me:

 - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if (self = [super initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:reuseIdentifier]) { } return self; } 
0
source

Source: https://habr.com/ru/post/1469408/


All Articles