2012-06-20 4 views
1

Salut tout le monde j'ai fait un UITableView dans mon application et quand une cellule est touchée, elle élargit le problème que je suis, il ne s'effondre pas, peu importe ce que j'ai essayé, je suis sûr que c'est quelque chose comprendre le seul moment où il s'effondre est quand une autre cellule est engagée.effondrement un UITableView

Voici mon code:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{  
    selectedCellIndexPath = indexPath; 

    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; 

    if (selectedCellIndexPath) { 
     selected = YES; 
    } 
} 

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{   
    if(selectedCellIndexPath != nil 
     && [selectedCellIndexPath compare:indexPath] == NSOrderedSame) 
     return 150; 

    return 44; 
} 
+0

Vérifiez le lien suivant: - http://stackoverflow.com/questions/8657502/expandable-tableview/8697503#8697503 –

Répondre

1

Vous changez jamais selectedCellIndexPath-nil, de sorte que la ligne actuelle sélectionnée ne soit pas modifié jusqu'à ce qu'un nouveau est sélectionné. Dans didSelectRowAtIndexPath vous devez modifier le début à ce qui suit:

if (selectedCellIndexPath == indexPath) 
    selectedCellIndexPath = nil; 
else 
    selectedCellIndexPath = indexPath; 
+0

okay je vais essayer que –

+0

désolé mais cela n'a pas fonctionné –

0
In selectedCellIndexPath you are storing pointer of indexPath. It can change when you reload table, means indexPath object of same cell can be different when you select it second time. 
It's safer if you store indexPath.section & indexPath.row 



     -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
     {  
       if(_section == indexPath.section && _row == indexPath.row) 
       { 
        _section = -1; 
        _row = -1 
       } 
       else 
       { 
        _section = indexPath.section; 
        _row = indexPath.row; 
       } 
       [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone]; 

       if (selectedCellIndexPath) {//change it as per your requirement 
        selected = YES; 
       } 
     } 

     -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
     {   
       if(_section == indexPath.section && _row == indexPath.row) 
        return 150; 

       return 44; 
     } 
0

Lorsque vous appelez la fonction de rechargement de ligne, entrez-vous dans la fonction de délégué cellForRow? Si c'est le cas, vous devriez mettre quelques fonctionnalités pour réduire les lignes après avoir vérifié la ligne sélectionnée.