2011-07-11 29 views
6

Vi que hay muchos recursos disponibles en la red con respecto a esta pregunta. Debo cargar un archivo XIB diferente (UIViewContoller) para mi celda. Diseñé mi celda y deseo cargar ese diseño en mi celda.Cómo cargar el archivo xib para la celda en la vista de tabla en iphone

Escribí este código pero recibo una excepción.

-[UIView setTableViewStyle:]: unrecognized selector sent to instance 0x6040de0 
2011-07-11 14:42:27.461 TableViewTest[2776:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setTableViewStyle:]: unrecognized selector sent to instance 0x6040de0' 

Y aquí es mi código de archivo de punta de carga

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    // Load the top-level objects from the custom cell XIB. 
    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"loadXibfile" owner:self options:nil]; 
    // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain). 
    cell = [topLevelObjects objectAtIndex:0]; 
} 


return cell; 
+0

http://adeem.me/blog/2009/05/30/iphone-sdk-tutorial-part-6-creating-custom-uitableviewcell-using-interface-builder-uitableview/ Este es buen tutorial para esta pregunta. –

Respuesta

14

Debe utilizar UITableViewCell y no UIView en su archivo XI ter.

+1

+1 me ayudó. por qué esta gente no acepta la respuesta correcta. –

2

Aquí está la solución y es útil para mí y el hogar para usted o algún otro puede ser.

cell = [[[YourCustomcell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"] autorelease]; 
     NSArray *toplavelobject=[[NSBundle mainBundle]loadNibNamed:@"YourCustomcell" owner:self options:nil]; 
     for(id c in toplavelobject) 
     { 
      if ([c isKindOfClass:[UITableViewCell class]]) 
      { 
       cell=(YourCustomcell *) c; 
       break; 
      } 
     } 
0

Esto es lo que hice. De nuevo, parte de la técnica es bastante incómoda y necesito una mejora.

Primero creé una nueva subclase de UITableViewCell. El problema es que no tengo la opción de marcar "incluir" xib. Es como si xib solo fuera para UIViewcontroller. Supongo que puede crear una subclase de UIViewController con XIB y luego crear otra subclase de UITableViewCell y mover la plantilla a su subclase de UIViewController.

Trabajos.

Entonces ponga estas funciones:

@implementation BGCRBusinessForDisplay2 

- (NSString *) reuseIdentifier { 
    return [[self class] reuseIdentifier]; 
}; 
+ (NSString *) reuseIdentifier { 
    return NSStringFromClass([self class]); 
}; 

Para inicializar hago:

- (BGCRBusinessForDisplay2 *) initWithBiz: (Business *) biz 
{ 
    if (self.biz == nil) //First time set up 
    { 
     self = [super init]; //If use dequeueReusableCellWithIdentifier then I shouldn't change the address self points to right 
     NSString * className = NSStringFromClass([self class]); 
     PO (className); 
     [[NSBundle mainBundle] loadNibNamed:className owner:self options:nil]; 
     [self addSubview:self.view]; //What is this for? self.view is of type BGCRBusinessForDisplay2. That view should be self, not one of it's subview Things don't work without it though 
    } 
    if (biz==nil) 
    { 
     return self; //Useful if we only want to know the height of the cell 
    } 

    self.biz = biz; 
    self.Title.text = biz.Title; //Let's set this one thing first 
    self.Address.text=biz.ShortenedAddress; 

El [self addSubview:self.view]; es un poco torpe. Es lo que otros dicen que debería hacer y no funcionará sin eso. En realidad, quiero que self.view sea uno mismo, en lugar de una subvista de uno mismo. Pero hei ... No sé cómo hacerlo de lo contrario. ...

Entonces implementar esto para cellForRowAtIndexPath

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

    //[FetchClass singleton].FetchController 
    if([BGMDCRFetchClass singleton].FetchController.fetchedObjects.count!=0){ 

     BGCRBusinessForDisplay2 *cell = (BGCRBusinessForDisplay2*)[tableView dequeueReusableCellWithIdentifier:[BGCRBusinessForDisplay2 reuseIdentifier]]; 

     if (cell == nil) 
     { 
      cell =[BGCRBusinessForDisplay2 alloc]; 
     } 
     else{ 
      while (false); 
     } 

     Business * theBiz=[[BGMDCRFetchClass singleton].FetchController objectAtIndexPath:indexPath]; 
     cell = [cell initWithBiz:theBiz]; 

     return cell; 
     //return theBiz.CustomCell; 
    }else{ 
     UITableViewCell * tvc=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"tvc"]; 
     return tvc; 
    } 
} 

en cuenta que separo alloc desde init. Eso es un poco incómodo. Es por eso que en mi - (BGCRBusinessForDisplay2 *) initWithBiz: (Business *) biz si una celda se ha inicializado antes, simplemente no hago la parte superior de la inicialización. Simplemente asigno valores de Business * a los diversos puntos de venta en BGCRBusinessForDisplay2.

Si alguien puede mejorar mis respuestas, son bienvenidos. Hasta ahora funciona.

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

    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d",indexPath.row]; 
    LatestNewsCell *cell = (LatestNewsCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 

     cell = [[[NSBundle mainBundle] loadNibNamed:@"LatestNewsCell" owner:self options:nil] objectAtIndex:0]; 
    } 
} 
Cuestiones relacionadas