Returning "I" until it is set as a result of "[(super or self) init ...]" when I initialize the user cell

In CustomCell.m, I define an init method where I want to load a cell from 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 MyTableViewController.m in the cellForRowAtIndexPath method, 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 got Returning 'self' while it is not set to the result of '[(super or self) init...]'
What am I doing wrong?

+6
source share
4 answers

You rewrite self (returned from super init ) with the object returned from your array. If you want to load a custom cell from nib, do it in your cellForRowAtIndexPath method or create a convenient class method in your custom cell, which is loaded from nib:

In your cellForRowAtIndexPath:

 cell = [CustomCell cell]; 

In the implementation of your cell:

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

EDIT - the method name has changed, since new * indicates that the saved object will be saved.

+8
source

Save the init method as shown below and follow the link 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; } } } } 
+7
source

What i do

 -(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 works fine, and Product -> Analyse does not give any warnings or errors.

+1
source

I met the same problem, I fixed it by deleting code similar to

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

from the initialization method for defining a CustomView.

Put this code in the place where you create Custom.

+1
source

Source: https://habr.com/ru/post/906291/


All Articles