2009-08-28 9 views
29

Je sais comment personnaliser la tableViewCell.Comment personnaliser tableView Voir la section - iPhone

J'ai vu de nombreuses applications personnalisant la tableView Cell.

De même, je souhaite personnaliser TableView section en-tête

« Supposons - Un nom de section doit être différente police, il a différentes images de fond, etc. »

Est-ce possible? Comment?

Dans quelle méthode Dois-je implémenter le code?

+2

+1 pour la belle question. :) – mAc

Répondre

60

Au lieu d'utiliser la méthode normale

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

Vous voulez mettre en œuvre celui-ci:

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

Comme vous pouvez le voir, le second retourne un UIView au lieu de simplement la chaîne pour le texte. Vous pouvez donc personnaliser votre propre vue (avec des étiquettes, etc.) et la renvoyer.

Voici un exemple de la façon dont vous pourriez faire (être mis en œuvre dans la méthode ci-dessus):

// 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; 

espoir qui aide

+1

Si votre table avait deux sections par exemple, comment référenceriez-vous cette deuxième section de table, donc l'en-tête de section 1 aurait un headerLabel.text différent de l'en-tête de section 2? – iamtoc

+1

Par l'entier qui est passé dans la méthode. –

+2

Et si je veux seulement avoir un en-tête pour la section = 1 et non pour la section = 0? J'ai un problème avec ce problème ... – jsetting32

Questions connexes