2010-02-27 4 views
0

J'écris une application pour l'entreprise de mon père, il s'agit de listes, cependant, j'ai vu qu'il semble réorganiser la liste, même si je ne le veux pas. Par exemple, toute la section 1 est censée dire une chose, quand je défile vers le bas, le contenu des autres sections est mis dans les cellules de la section 1, cela vaut pour les autres sections aussi. Voici tout le code de la table, si ça aide.Les cellules se réorganisent sur iPhone?

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    if (section == ENGINEER_ID) { 
     return 3; 
    } else if (section == FIREMAN_ID) { 
     return 3; 
    } else if (section == CONDUCTOR_ID) { 
     return 3; 
    } else if (section == TRAINMASTER_ID) { 
     return 3; 
    } else { 
     return 0; 
    } 
} 
// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
     if ([indexPath section] == ENGINEER_ID) { 
      cell.textLabel.text = @"Engineer"; 
     } else if ([indexPath section] == FIREMAN_ID) { 
      cell.textLabel.text = @"Fireman"; 
     } else if ([indexPath section] == CONDUCTOR_ID) { 
      cell.textLabel.text = @"Conductor"; 
     } else if ([indexPath section] == TRAINMASTER_ID) { 
      cell.textLabel.text = @"Trainmaster"; 
     } 
    } 

    // Configure the cell. 

    return cell; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {  
    if (section == ENGINEER_ID) { 
     return @"Master Engineer II"; 
    } else if (section == FIREMAN_ID) { 
     return @"Fireman"; 
    } else if (section == CONDUCTOR_ID) { 
     return @"Conductor"; 
    } else if (section == TRAINMASTER_ID) { 
     return @"Trainmaster"; 
    } else { 
     return @"What."; 
    } 

} 

Répondre

1

Vous devez déplacer l'affectation du texte du conditionnel. Après que les 3 premières cellules sont alloc/init, vous n'obtiendrez pas forcément plus de cellules.

Vous affectez cell.textLabel.text uniquement si vous créez une nouvelle cellule.

Logic doit être:

NSString statique * CellIdentifier = @ "Cell";

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

if ([indexPath section] == ENGINEER_ID) { 
    cell.textLabel.text = @"Engineer"; 
} else if ([indexPath section] == FIREMAN_ID) { 
    cell.textLabel.text = @"Fireman"; 
} else if ([indexPath section] == CONDUCTOR_ID) { 
    cell.textLabel.text = @"Conductor"; 
} else if ([indexPath section] == TRAINMASTER_ID) { 
    cell.textLabel.text = @"Trainmaster"; 
} 

Et bien sûr, après vous vous lassez de l'écriture en cascade si les déclarations, vous pouvez utiliser l'interrupteur ([section indexPath])

Questions connexes