Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning 'self' while it is not set to the result of '[(super or self) init...]' when I initialize custom cell

In CustomCell.m I define init method where I want to load cell from the IB:

- (id)init {
    self = [super init];
    if (self) {
        NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        self = [nib objectAtIndex:0];

    }
    return self;
}

In the MyTableViewController.m in the method cellForRowAtIndexPath I initialize my custom cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

cell=[[CustomCell alloc]init];
return cell;

}

Everything works as I expected but when I did Product -> Analyse I get
Returning 'self' while it is not set to the result of '[(super or self) init...]'
What am I doing wrong?

like image 545
Alexey Avatar asked Jan 18 '12 11:01

Alexey


4 Answers

You are overwriting self (returned from super init) with the object returned from your array. If you want to load a custom cell from a nib, do it in your cellForRowAtIndexPath method, or create a convenience class method on your custom cell that loads from the nib:

In your cellForRowAtIndexPath:

cell = [CustomCell cell];

In your cell's implementation:

+(CustomCell*)cell
{
    NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];         
    return [nib objectAtIndex:0];
}

EDIT - changed method name since new* indicates that a retained object will be returned.

like image 162
jrturton Avatar answered Nov 08 '22 21:11

jrturton


Keep your init method as below, and do the linking in the Interface Builder

- (id)init {
    self = [super init];
    if (self) {

    }
    return self;
}

And

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomCell";

    CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];

        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (CustomCell *) currentObject;
                break;
            }
        }
    }
}
like image 27
sElanthiraiyan Avatar answered Nov 08 '22 23:11

sElanthiraiyan


What I am doing is

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) 
    {
        // Initialization code.
        //
        UITableViewCell *view = [[[NSBundle mainBundle] loadNibNamed:@"SmallCellView" owner:self options:nil] lastObject];
        self.backgroundView = view;
}
    return self;
}

then in the main class

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    SmallCellView *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[SmallCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
 }
  return cell;
}

For me this is working fine and in Product -> Analyse is not giving any warning or error

like image 1
Krrish Avatar answered Nov 08 '22 21:11

Krrish


I met the same problem, I have fixed it by removing the code that like

NSArray *nib =[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];         
return [nib objectAtIndex:0];

from the CustomView's definition init Method.

Put these code the at the place where you create the Custom.

like image 1
Barry Wang Avatar answered Nov 08 '22 21:11

Barry Wang