Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tableview subtitles on IOS7

I'm trying to add a subtitle to my tableview cells, but they are not displayed. Where is the mistake?

Is the row [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle] up-to-date, also with iOS 7?

Best regards

Frank


- (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;
}
like image 692
Sprengepiel Avatar asked Oct 30 '13 21:10

Sprengepiel


2 Answers

This code:

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

will never execute because dequeueReusableCellWithIdentifier: forIndexPath: is guaranteed to allocate a new cell.

Unfortunately, registerClass:forCellReuseIdentifier: doesn't let you specify a UITableViewCellStyle.

Change dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath to simply dequeueReusableCellWithIdentifier:CellIdentifier. This method does not guarantee a cell will be returned.* When it's not, your code will then create a new cell with the style you want.


* - (It will if you're using a storyboard, as rdelmar points out, but that's not the case here.)

like image 161
Aaron Brager Avatar answered Nov 16 '22 06:11

Aaron Brager


- (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;
}
like image 1
Iya Avatar answered Nov 16 '22 08:11

Iya