2012-04-29 15 views
5

Estoy tratando de obtener una UITableView para mostrar un headerView.xib que he creado, pero después de compilarlo, no se muestra nada. Desea que UITableView se pueda editar y haya agregado tres métodos al archivo ItemsViewController.m, pero no se muestra nada.UITableView no mostrará headerView.xib

¿Qué pasa? Gracias por adelantado.

Éstos son los archivos relevantes:

ItemsViewController.h

#import <Foundation/Foundation.h> 

@interface ItemsViewController : UITableViewController 
{ 
    IBOutlet UIView *headerView; 
} 

-(UIView *)headerView; 
-(IBAction)addNewItem:(id)sender; 
-(IBAction)toggleEditingMode:(id)sender; 

@end 

ItemsViewController.m

#import "ItemsViewController.h" 
#import "BNRItemStore.h" 
#import "BNRItem.h" 

@implementation ItemsViewController // Incomplete implementation 

-(id)init 
{ 
    // Call the superclass's designated initializer 
    self = [super initWithStyle:UITableViewStyleGrouped]; 
    if (self) { 
    for(int i = 0; i < 5; i++) { 
     [[BNRItemStore sharedStore]createItem]; 
    } 

    } 
    return self; 
} 

-(id)initWithStyle:(UITableViewStyle)style 
{ 
    return [self init]; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [[[BNRItemStore sharedStore]allItems]count]; 
} 

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


    // Check for a reusable cell first, use that if it exists 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; 

    // If there is no reusable cell of this type, create a new one 
    if (!cell) { 
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"]; 
    } 

    // Set the text on the cell with the description of the item 
    // that is at the nth index of items, where n = row this cell 
    // will appear in on the tableview 
    BNRItem *p = [[[BNRItemStore sharedStore]allItems]objectAtIndex:[indexPath row]]; 

    [[cell textLabel]setText:[p description]]; 

    return cell; 
} 

-(UIView *)headerView 
{ 
    // If we haven't loaded the headerView yet 
    if (!headerView) { 
    //Load HeaderView.xib 
    [[NSBundle mainBundle]loadNibNamed:@"HeaderView" owner:self options:nil]; 
    } 
    return headerView; 
} 

-(UIView *)tableView:(UITableView *)tv viewForHeaderInSection:(NSInteger)sec 
{ 
    return [self headerView]; 
} 

-(CGFloat)tableView:(UITableView *)tv heightForHeaderInSection:(NSInteger)sec 
{ 
    // The height of the header view should be determined from the height of the 
    // view in the XIB file 
    return [[self headerView]bounds].size.height; 
} 



@end 
+0

En el xib del encabezado, ¿se establece una conexión entre la vista del encabezado y el propietario del archivo? El propietario del archivo también debe tener su clase configurada en IB para ItemsViewController. –

+0

Sí, ese era mi problema. Ahora funciona bien. Gracias. – pdenlinger

Respuesta

3

Uno o ambos de los siguientes elementos no están configurados correctamente. En primer lugar, el objeto Propietario del archivo en HeaderView.xib necesita tener su clase establecida en ItemsViewController. Después de eso, la toma headerView debe estar conectada desde el propietario del archivo a la vista de nivel superior en el xib.

0

Parece significativo que el método loadNibNamed interior de su método headerView devuelve una matriz de el contenido del archivo nib, pero no ha hecho nada con esos contenidos. Tal vez debería encontrar la vista dentro de los objetos no archivados y establecer su headerView en la vista correspondiente desde el interior del archivo de punta.

0

¿Echas de menos implementar estos métodos? No estoy seguro de que esta clase ya haya establecido el valor predeterminado de la sección = 1.

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
3

tuve el mismo problema y se resolvió cambiando la línea de código bajo el comentario // Cargar HeaderView.xib a

// Load HeaderView.xib 
    headerView = [[[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil] lastObject]; 

el archivo semilla tiene un solo punto de vista, así que asignarle el último (sólo) vista.

Espero que esto ayude.

Cuestiones relacionadas