Tableview subtitles on iOS7

I am trying to add subtitles to tableview cells, but they are not displayed. Where is the mistake?

Is the string [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle] updated, also with iOS 7?

Best wishes

Franc


 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } cell.textLabel.text = @"TestA"; cell.detailTextLabel.text = @"TestB"; return cell; } 
+8
ios objective-c uitableview ios7
source share
4 answers

This code:

 if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } 

will never be executed because dequeueReusableCellWithIdentifier: forIndexPath: guaranteed to dequeueReusableCellWithIdentifier: forIndexPath: new cell.

Unfortunately, registerClass:forCellReuseIdentifier: does not allow you to specify a UITableViewCellStyle .

Change dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath to just dequeueReusableCellWithIdentifier:CellIdentifier . This method does not guarantee that the cell will be returned. * When this is not the case, your code will then create a new cell with the style you want.


* - (This will be if you use the storyboard as rdelmar points out, but it is not).

+5
source share
 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = nil; cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"]; } cell.textLabel.text = @"Title1"; cell.detailTextLabel.text = @"Subtitle 1"; return cell; } 
+1
source share

I had a similar problem and no solution on the internet worked for me. Turns out I was an idiot. I will lay out my decision simply if someone else comes across a similar scenario.

I assume you are using a storyboard , created a prototype and set the subtitle style

In the dropdown list of layouts make sure you select the protoptype cell and select the subtitles. See Image:

enter image description here


Now make sure the font color of the label has the character that we see on your background!

0
source share

Just subclass UITableViewCell and override

 - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier; 

with the first line

 [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier]; 

And register the cell class as a table

 [tableView registerClass:[YourCellSubclass class] forCellReuseIdentifier:@"YourCellID"]; 
0
source share

All Articles