2010-05-25 5 views
0

J'ai un UITableView avec 3 sections qui sont codées en dur. Tout fonctionne bien, mais je ne suis pas sûr si je le fais correctement.Quelle est la bonne façon de coder les sections dans un UITableView?

Définir le nombre de lignes dans la section:

- (NSInteger)tableView:(UITableView *)tblView numberOfRowsInSection:(NSInteger)section 
{ 
    NSInteger rows; 

     //Bio Section 
     if(section == 0){ 
      rows = 2; 
     } 
     //Profile section 
     else if(section == 1){ 
      rows = 5; 
     } 
     //Count section 
     else if(section == 2){ 
      rows = 3; 
     } 
    } 

    return rows; 
} 

Voici où je construis mes cellules:

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tblView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    cell.textLabel.numberOfLines = 5; 
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:(10.0)]; 
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap; 

    if ([self.message_source isEqualToString:@"default"]) { 
     if (indexPath.section == 0) { 
      if (indexPath.row == 0) { 
       cell.textLabel.text = [Utils formatMessage:[NSString stringWithFormat: @"%@", mySTUser.bio]]; 
       cell.detailTextLabel.text = nil; 
      } 
      else if(indexPath.row == 1){ 
       cell.textLabel.text = [NSString stringWithFormat: @"%@", mySTUser.website]; 
       cell.detailTextLabel.text = nil; 
       cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
      } 
     } 
} //more code exists, but you get the point... 

Maintenant, je définis mon nombre de sections

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tblView 
{ 
    return 3; 
} 

Est-ce la bonne façon de coder en dur mon UITableView? Vais-je rencontrer des problèmes lorsque les cellules sont réutilisées?

Répondre

3

Vous pouvez envisager d'utiliser un arbre switch-case avec un enumerated type, pour remplacer les conditions if qui testent divers entiers codés en dur. This blog post explique cette option plus en détail. En utilisant switch-case avec vos méthodes déléguées de vue de table rendra votre code beaucoup plus lisible et flexible. Sinon, votre code de réutilisation semble correct.

+0

BEAUCOUP BEAUCOUP DE NETTOYANT. Je vous remercie! –

+0

Great link. Merci. –

Questions connexes