2012-01-18 6 views
6

En CustomCell.m Defino el método init donde quiero cargar la celda del IB:Volviendo 'self' mientras no está configurado para el resultado de '[(super o self) init ...]' cuando inicializo la celda personalizada

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

    } 
    return self; 
} 

en el MyTableViewController.m en el cellForRowAtIndexPath método que inicializar mi celda personalizado

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

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

}

Todo funciona como esperaba, pero cuando lo hice me Product -> Analyse obtener
Returning 'self' while it is not set to the result of '[(super or self) init...]'
¿Qué estoy haciendo mal?

Respuesta

8

Está sobrescribiendo self (devuelto desde super init) con el objeto devuelto por su matriz. Si desea cargar una celda personalizado a partir de una semilla, hacerlo en su método cellForRowAtIndexPath, o crear un método de clase de conveniencia en tu celular a medida que se carga desde la punta:

En su cellForRowAtIndexPath:

cell = [CustomCell cell]; 

en la ejecución de su celular:

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

EDITAR - cambiado el nombre del método desde nuevo * indica que se devolverá un objeto retenido.

+0

Gracias, pero antes cell = [CustomCell newCell]; ¿Necesito la célula de inicio: célula = [[CustomCell alloc] init]; – Alex

+0

¡Hice como usted explicó, y ahora tengo esto! en lugar de la anterior: El objeto con +0 retendrá los conteos devueltos a la persona que llama donde se espera un recuento de retención +1 (propietario) – Alex

+0

Oh sí, lo llamé newCell de lo que ARC se quejará. Actualizaré la respuesta, lo siento. – jrturton

7

Mantenga su método de inicio como el de abajo, y hacer de la vinculación en el Interface Builder

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

    } 
    return self; 
} 

Y

- (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; 
      } 
     } 
    } 
} 
+1

Gracias por la respuesta, conozco este método, pero prefiero tener el método init dentro de la celda. – Alex

1

Lo que estoy haciendo es

-(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; 
} 

entonces en la principal clase

- (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; 
} 

Para mí esto está funcionando bien y en Product -> Analyse no está dando ninguna advertencia o error

1

me encontré con el mismo problema, me he fijado que al eliminar el código que le gusta

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

de la definición del CustomView Método init.

Ponga este código en el lugar donde crea la Personalización.

Cuestiones relacionadas