2010-12-13 56 views
0

J'ai un TableView avec des cellules personnalisées.En-têtes de section de table pour iPhone

Je dois être en mesure de mettre 2 en-têtes de section appelée Instruction et référence.

J'ai besoin de 7 lignes pour la section Instruction et 3 lignes pour la section Référence.

Voici comment j'ai codé ma vue de table. Je sais que ce n'est pas idéal mais je suis nouveau à ceci:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
return 1;} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 

return [listOfItems count];} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
    cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
} 

switch (indexPath.row) { 
    case 0: 
     cell.customLabel.text = @"Anatomy"; 
     cell.customImage.image = [UIImage imageNamed:@"anatomy.png"]; 
     cell.arrowImage.image = [UIImage imageNamed:@"arrow.png"]; 

     break; 
    case 1: 
     cell.customLabel.text = @"Indications"; 
     cell.customImage.image = [UIImage imageNamed:@"Info.png"]; 
     cell.arrowImage.image = [UIImage imageNamed:@"arrow.png"]; 

     break; 
    case 2: 
     cell.customLabel.text = @"Medications"; 
     cell.customImage.image = [UIImage imageNamed:@"pill.png"]; 
     cell.arrowImage.image = [UIImage imageNamed:@"arrow.png"]; 

     break; 
    case 3: 
     cell.customLabel.text = @"Equipment"; 
     cell.customImage.image = [UIImage imageNamed:@"spanner.png"]; 
     cell.arrowImage.image = [UIImage imageNamed:@"arrow.png"]; 
     break; 
    case 4: 
     cell.customLabel.text = @"Procedure"; 
     cell.customImage.image = [UIImage imageNamed:@"procedure.png"]; 
     cell.arrowImage.image = [UIImage imageNamed:@"arrow.png"]; 
     break; 
    case 5: 
     cell.customLabel.text = @"Complications"; 
     cell.customImage.image = [UIImage imageNamed:@"complication.png"]; 
     cell.arrowImage.image = [UIImage imageNamed:@"arrow.png"]; 
     break; 
    case 6: 
     cell.customLabel.text = @"Procedure Video"; 
     cell.customImage.image = [UIImage imageNamed:@"procedure.png"]; 
     cell.arrowImage.image = [UIImage imageNamed:@"arrow.png"]; 
     break; 
    case 7: 
     cell.customLabel.text = @"Calculations"; 
     cell.customImage.image = [UIImage imageNamed:@"math.png"]; 
     cell.arrowImage.image = [UIImage imageNamed:@"arrow.png"]; 
     break; 
    case 8: 
     cell.customLabel.text = @"Videos & Images"; 
     cell.customImage.image = [UIImage imageNamed:@"Pin.png"]; 
     cell.arrowImage.image = [UIImage imageNamed:@"arrow.png"]; 
     break; 
    case 9: 
     cell.customLabel.text = @"Feedback"; 
     cell.customImage.image = [UIImage imageNamed:@"feedback.png"]; 
     cell.arrowImage.image = [UIImage imageNamed:@"arrow.png"]; 
     break; 
    default: 
     break; 
} 
return cell;} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
if (indexPath.row == 0) { 
    AnatomyWeb1 *test1 = [[AnatomyWeb1 alloc] initWithNibName:@"AnatomyWeb1" bundle:nil]; 

    [self.navigationController pushViewController:test1 animated:YES]; 

    [test1 release]; 
} 
if (indexPath.row == 1) { 
    Indications *test1 = [[Indications alloc] initWithNibName:@"Indications" bundle:nil]; 

    [self.navigationController pushViewController:test1 animated:YES]; 

    [test1 release]; 
} 
if (indexPath.row == 2) { 
    Medication *test6 = [[Medication alloc] initWithNibName:@"Medication" bundle:nil]; 

    [self.navigationController pushViewController:test6 animated:YES]; 

    [test6 release]; 
} 
if (indexPath.row == 3) { 

    ProcedureWeb1 *test3 = [[ProcedureWeb1 alloc] initWithNibName:@"ProcedureWeb1" bundle:nil]; 

    [self.navigationController pushViewController:test3 animated:YES]; 

    [test3 release]; 
} 
if (indexPath.row == 4) { 
    ProcedureWeb3 *test3 = [[ProcedureWeb3 alloc] initWithNibName:@"ProcedureWeb3" bundle:nil]; 

    [self.navigationController pushViewController:test3 animated:YES]; 

    [test3 release]; 
} 
if (indexPath.row == 5) { 
    Complications2 *test4 = [[Complications2 alloc] initWithNibName:@"Complications2" bundle:nil]; 

    [self.navigationController pushViewController:test4 animated:YES]; 

    [test4 release]; 
} 
if (indexPath.row == 6) { 
    [self video1]; 
} 
if (indexPath.row == 7) { 
    Calculations1 *test3 = [[Calculations1 alloc] initWithNibName:@"Calculations1" bundle:nil]; 

    [self.navigationController pushViewController:test3 animated:YES]; 

    [test3 release]; 

} 
if (indexPath.row == 8) { 
    VideosAndImages *test3 = [[VideosAndImages alloc] initWithNibName:@"VideosAndImages" bundle:nil]; 

    [self.navigationController pushViewController:test3 animated:YES]; 

    [test3 release]; 

} 
if (indexPath.row == 9) { 
    Feedback *test11 = [[Feedback alloc] initWithNibName:@"Feedback" bundle:nil]; 

    test11.previousViewController=self; 

    test11.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 

    [self presentModalViewController:test11 animated:YES]; 

    [test11 release]; 
}} 

Merci pour les suggestions les gars.

+2

Vous devez coller le code (plutôt qu'une capture d'écran) dans la question, sélectionnez-le et marquez-le comme code (l'icône "binaire" dans l'éditeur). :-) –

+0

Pourquoi avez-vous posté des captures d'écran au lieu du code? – vikingosegundo

+0

Je posterai le code quand je rentrerai à la maison, juste en marchant vers ma voiture. Désolé, je pensais qu'il y avait peut-être trop de code à poster. – StefanHanotin

Répondre

2

De même que vous avez utilisé indexPath.row, vous devez utiliser indexPath.section pour identifier la section actuellement active. Par exemple, dans cellForRowAtIndexPath, vous pouvez avoir deux instructions switch imbriquées, une pour la section, une pour la ligne.

remplir l'autre procédé de tableView (sans prétendre intégralité ici):

numberOfSectionsInTableView retournera 2 (deux sections), numberOfRowsInSection retournera 7 ou 3 en fonction de la section active, titleForHeaderInSection sera de retour (si nécessaire) la chaîne que vous voulez voir en haut de votre section.

Et sur une note personnelle: vous ne voulez pas appeler votre vue test1, mieux déclarer comme

AnatomyWeb * anatomyWeb = ... 
Indications * indications = ... 

Vous utilisez temps plusieurs test3 et un style de nommage « Testx » peut être douleur debug

+0

Salut Olaf, Merci pour la réponse. Je ne sais pas comment l'intégrer car je n'ai aucune idée des sections actuellement actives, etc. Pouvez-vous nous en dire plus? Merci aussi pour les conseils concernant mes conventions de nommage. Je dois vraiment les changer! :) – StefanHanotin

+0

par exemple la tableview appellera automatiquement '- (NSInteger) tableView: (UITableView *) tableView numéroOuRowsInSection: (NSInteger) section'. Ajoutez un commutateur ou un cas pour détecter quelle section est active. Similaire dans '- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath'. indexPath a deux champs: ligne et section. – Olaf

+0

Merci un million pour votre aide Olaf. Est-ce que exactement ce que vous avez dit et mon problème est résolu. Maintenant pour ces conventions de nommage! – StefanHanotin