2009-08-28 9 views
29

Sé cómo personalizar tableViewCell.Cómo personalizar la vista de sección de TableView - iPhone

He visto muchas aplicaciones personalizando la celda tableView.

Del mismo modo, quiero personalizar TableView Sección Cabecera

"Supongamos - Un nombre de sección debe estar en fuente diferente, tiene diferentes imágenes de fondo, etc."

¿Es posible cómo?

¿En qué método debo implementar el código?

+2

1 para la buena pregunta. :) – mAc

Respuesta

60

En lugar de utilizar el método normal

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 

desea implementar esta:

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 

Como se puede ver, la segunda devuelve un UIView en lugar de sólo la cadena de texto. Por lo tanto, puede personalizar su propia vista (con etiquetas, etc.) y devolverla.

Aquí es un ejemplo de cómo se puede hacer eso (que se implementa en el método anterior):

// create the parent view that will hold header Label 
UIView* customView = [[[UIView alloc] initWithFrame:CGRectMake(10,0,300,60)] autorelease]; 

// create image object 
UIImage *myImage = [UIImage imageNamed:@"someimage.png"];; 

// create the label objects 
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease]; 
headerLabel.backgroundColor = [UIColor clearColor]; 
headerLabel.font = [UIFont boldSystemFontOfSize:18]; 
headerLabel.frame = CGRectMake(70,18,200,20); 
headerLabel.text = @"Some Text"; 
headerLabel.textColor = [UIColor redColor]; 

UILabel *detailLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease]; 
detailLabel.backgroundColor = [UIColor clearColor]; 
detailLabel.textColor = [UIColor darkGrayColor]; 
detailLabel.text = @"Some detail text"; 
detailLabel.font = [UIFont systemFontOfSize:12]; 
detailLabel.frame = CGRectMake(70,33,230,25); 

// create the imageView with the image in it 
UIImageView *imageView = [[[UIImageView alloc] initWithImage:myImage] autorelease]; 
imageView.frame = CGRectMake(10,10,50,50); 

[customView addSubview:imageView]; 
[customView addSubview:headerLabel]; 
[customView addSubview:detailLabel]; 

return customView; 

Espero que ayude

+1

Si tu tabla tuviera dos secciones, por ejemplo, ¿cómo harías referencia a esa segunda sección de tabla, por lo que el encabezado de sección 1 tendría un encabezado de etiqueta diferente del encabezado de sección 2? – iamtoc

+1

Por el número entero que pasó al método. –

+2

¿Qué sucede si solo quiero tener un encabezado para section = 1 y no para section = 0? Tengo un problema con este problema ... – jsetting32

Cuestiones relacionadas