0

I instantiate a UITableView in a UIViewController when the view is loaded as :

table = [[UITableView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x,self.view.frame.origin.y + hauteurFavorisCell, self.view.frame.size.width, self.view.frame.size.height-hauteurFavorisCell-hauteurNavigationBar)];

[table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"DetailsFavoris"];
table.delegate = self;
table.dataSource = self;
table.hidden = YES;
[self.view addSubview:table];

The problem is that I want the cell to have style : UITableViewCellStyleValue1. Because the cells are create with the method initWithStyle: UITableViewCellStyleDefault. I can't use dequeueReusableCellWithIdentifier:CellIdentifier. My code is thus :

static NSString *CellIdentifier = @"DetailsFavoris";

UITableViewCell *cell ;//= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}

...

return cell;

But I would like to reuse the cells to be more efficient. I wrote down :

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    // ignore the style argument, use our own to override
    self = [super initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
    if (self) {
        // If you need any further customization
    }
    return self;
}

But I got an error : No visible @interface for 'UIViewController' declares the selector initWithStyle:reuseIdentifier:.

What am I doing wrong? I checked on other answers but I found nothing.

meda
  • 45,103
  • 14
  • 92
  • 122
yaniki
  • 1
  • 2
  • Where are you implementing your `initWithStyle:…`? The error suggests that you are doing it inside a subclass of UIViewController instead of a subclass of UITableViewCell. – Phillip Mills Apr 22 '14 at 17:00
  • Indeed, I wrote it on my subclass of UIVIewController. When I instantiate my UItableView 'table' in the ViewDidLoad, it creates cells with default style automatically. Where am I supposed to write the initWithStyle method then? – yaniki Apr 23 '14 at 07:09

1 Answers1

1

Move this code

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    // ignore the style argument, use our own to override
    self = [super initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
    if (self) {
        // If you need any further customization
    }
    return self;
}

To your UITableViewCell subclass. Because this method related to UITableViewCell class, not to UIViewController.

skywinder
  • 21,291
  • 15
  • 93
  • 123