Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell subtitle. Not getting it to work

Using Xcode 4.6, I am trying to display a typical UITableView with its cells with subtitles.

If I am not wrong, the code is this:

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

Test *myTest = [self listTests][indexPath.row];

cell.textLabel.text = [myTest name];

UIFont *cellFont = [UIFont systemFontOfSize:16.0];
cell.textLabel.font = cellFont;

UIFont *detailFont = [UIFont systemFontOfSize:12.0];
NSMutableString *detailText = [NSMutableString stringWithFormat:@"%d", [myTest numQuestions]];
[detailText appendString:@" preguntas."];

cell.detailTextLabel.text = detailText;
cell.detailTextLabel.font = detailFont;

return cell;

}

For some reason, it never passes through this line of code:

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

so the cell is never initialized with UITableViewCellStyleSubtitle.

It is somehow getting ALWAYS FROM THE BEGINING a valid cell when doing [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

What can I be doing wrong?

It is really weard. I always use this code and it normally works. What else can I be doing wrong somewhere else?

like image 448
ElPiter Avatar asked Jan 15 '23 00:01

ElPiter


1 Answers

This happens when your cell is defined using a storyboard prototype. In this case the reusable cells are pre-created using the initWithCoder: method, so if (cell == nil) never gets hit. See this question for more information.

Since it appears that you would like to use a cell with a standard style, changing the table to not use a storyboard prototype or setting the prototype to "Subtitle" should fix this problem.

like image 73
Sergey Kalinichenko Avatar answered Jan 19 '23 19:01

Sergey Kalinichenko