2013-03-09 11 views
2

Dans mon application, j'ai un UITableView et j'ai deux UITableViewCell personnalisés différents. UITableView est initialement chargé avec un type de UITableViewCell personnalisé. Lorsque l'une des cellules est sélectionnée dans mon UITableView, je voudrais changer la cellule qui a été sélectionnée avec l'autre type de UITableViewCell personnalisé. Est-ce possible? Laisse-moi entendre ce que tu as.Modification de UITableViewCell lors de la sélection

Merci, NSNolan

#pragma mark - UITableView delegate methods 

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.array count]; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    if(indexPath.row == [self currentCellIndex]) { 
     [self setCurrentCellIndex:-1]; 

     NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"NewCell" owner:self options:nil]; 

     OldCell *cell = (OldCell *)[tableView cellForRowAtIndexPath:indexPath]; 
     cell = [nibs objectAtIndex:0]; 
    } 
    else { 
     [self setCurrentCellIndex:indexPath.row]; 

     NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"NewCell" owner:self options:nil]; 

     NewCell *cell = (NewCell *)[tableView cellForRowAtIndexPath:indexPath]; 
     cell = [nibs objectAtIndex:0]; 
    }   
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *cellIdentifier = @"OldCell"; 

    OldCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if (cell == nil) { 
     NSArray *nibs = [[NSBundle mainBundle] loadNibNamed:@"OldCell" owner:self options:nil]; 
     cell = [nibs objectAtIndex:0]; 
    } 

    return cell; 
} 
+0

Y a-t-il un bloc de code particulier qui vous pose problème? http://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/TableView_iPhone/AboutTableViewsiPhone/AboutTableViewsiPhone.html#//apple_ref/doc/uid/TP40007451 –

+0

Je ne suis pas ce que vous demandez ou le pertinence du lien. – NSNolan

+0

Vous pouvez y parvenir en ayant une variable d'instance qui contient l'indexPath sélectionné en cours. Puis rechargez la tableview. Dans cellForRowAtIndexPath: vous pouvez vérifier si indexPath est égal à selectedIndexPath, puis initialiser la deuxième cellule personnalisée. – Anupdas

Répondre

6

Dans votre méthode didSelectRow..., vous devez mettre à jour un drapeau (stocké dans une variable d'instance) indiquant le nouveau "mode" de la cellule. Rechargez ensuite la ligne en appelant tableView reloadRowsAtIndexPaths:withRowAnimation: en passant dans l'indexPath sélectionné.

Dans votre méthode cellForRow..., utilisez l'indicateur pour déterminer lequel des deux types de cellules utiliser pour la ligne.

+0

merci @rmaddy. fonctionne parfaitement. – NSNolan

Questions connexes