Remplacer une cellule personnalisée A existante par une autre cellule personnalisée B en mode Accordion – En sélection

Supposons qu'il existe des cellules personnalisées A et B de hauteur différente .
La cellule personnalisée A est chargée par défaut sur UITableView. Lorsque l'user choisira la cellule A, il enlèvera cette cellule et appenda la cellule B à cette position et inversement . Il fera l'animation de redimensionnement en style Accordéon.

Pour ce faire, vous devriez avoir une propriété (ou une key si vous utilisez un dictionary) dans votre tableau de données pour garder une trace de la cellule que vous voulez à chaque indexPath, et utiliser une instruction if-else dans cellFroRowAtIndexPath pour déquiler la cellule correcte. Dans didSelectRowAtIndexPath, vous devez vérifier cette propriété, la définir sur la propriété opposée, puis recharger la table. Vous devez également implémenter heightForRowAtIndexPath et vérifier cette même propriété pour déterminer la hauteur à renvoyer.

Après l'édition:

Si vous avez juste besoin de garder une trace d'une cellule sélectionnée, alors créez une propriété (je l'appelle selectedPath) pour conserver cette valeur et vérifiez-la dans heightForRowAtIndexPath et cellForRowAtIndexPath. J'ai créé deux cellules dans le storyboard, une simple UITableViewCell et l'autre une cellule personnalisée de class RDCell. Je ne suis pas sûr si cela donne l'animation que vous voulez, mais essayez-la et voyez si elle est proche:

#import "TableController.h" #import "RDCell.h" @interface TableController () @property (strong,nonatomic) NSArray *theData; @property (nonatomic) NSIndexPath *selectedPath; @end @implementation TableController - (void)viewDidLoad { [super viewDidLoad]; self.theData = @[@"One",@"Two",@"Three",@"Four",@"Five",@"Six",@"Seven",@"Eight"]; self.selectedPath = [NSIndexPath indexPathForRow:-1 inSection:0]; [self.tableView reloadData]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.theData.count; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if ([self.selectedPath isEqual:indexPath]) { return 90; }else{ return 44; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if ([self.selectedPath isEqual:indexPath]) { RDCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RDCell" forIndexPath:indexPath]; cell.label.text = self.theData[indexPath.row]; return cell; }else{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; cell.textLabel.text = self.theData[indexPath.row]; return cell; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSIndexPath *oldPath = self.selectedPath; self.selectedPath = indexPath; [self.tableView reloadRowsAtIndexPaths:@[indexPath,oldPath] withRowAnimation:UITableViewRowAnimationAutomatic]; } 
  **Got this amazing Solution, its working great...** @implementation NetworkCentreTable { NSMutableArray *arr; BOOL chk; int onSelectCount; NSIndexPath *onSelectTrack; } - (void)viewDidLoad { [super viewDidLoad]; arr=[[NSMutableArray alloc] initWithObjects:@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",nil]; onSelectCount=0; static NSSsortingng *CellIdentifier1 = @"NetworkCell2"; UINib *nib = [UINib nibWithNibName:@"NetworkCentreCellBig" bundle:nil]; [self.tblNetworkCentre registerNib:nib forCellReuseIdentifier:CellIdentifier1]; } #pragma mark - #pragma mark Custom Network TableView delegate and Datasource -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [arr count]; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSSsortingng *CellIdentifier1 = @"NetworkCentreCell"; static NSSsortingng *CellIdentifier2 = @"NetworkCentreCellBig"; if(self.selectedRowIndex && indexPath.row == self.selectedRowIndex.integerValue) { NetworkCentreCell *cell = (NetworkCentreCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2]; UIViewController *controller=[[UIViewController alloc] initWithNibName:CellIdentifier2 bundle:nil]; cell=(NetworkCentreCell *)controller.view; return cell; } else { NetworkCentreCell *cell = (NetworkCentreCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1]; UIViewController *controller=[[UIViewController alloc] initWithNibName:CellIdentifier1 bundle:nil]; cell=(NetworkCentreCell *)controller.view; return cell; } } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { onSelectCount++; NSLog(@"num=%d",onSelectCount); self.selectedIndexPath = indexPath; [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation: UITableViewRowAnimationNone]; self.selectedRowIndex = [NSNumber numberWithInteger:indexPath.row]; [self.tblNetworkCentre deselectRowAtIndexPath:indexPath animated:YES]; //First we check if a cell is already expanded. //If it is we want to minimize make sure it is reloaded to minimize it back if( onSelectCount==1 ) { [tableView beginUpdates]; NSIndexPath *previousPath = [NSIndexPath indexPathForRow:self.selectedRowIndex.integerValue inSection:0]; self.selectedRowIndex = [NSNumber numberWithInteger:indexPath.row]; onSelectTrack=indexPath; [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:previousPath] withRowAnimation:UITableViewRowAnimationFade]; [tableView endUpdates]; } if(onSelectTrack.row!=indexPath.row) { [tableView beginUpdates]; [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:onSelectTrack] withRowAnimation:UITableViewRowAnimationFade]; [tableView endUpdates]; onSelectTrack=indexPath; onSelectCount=0; [self tableView:tableView didSelectRowAtIndexPath:onSelectTrack]; } if(self.selectedRowIndex.integerValue == indexPath.row && onSelectCount==2) { [tableView beginUpdates]; self.selectedRowIndex = [NSNumber numberWithInteger:-1]; [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; onSelectCount=0; [tableView endUpdates]; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if(self.selectedRowIndex && indexPath.row == self.selectedRowIndex.integerValue) { return 280; }else{ return 85; } } 

Vous pouvez appeler [yourTableview reloadData] dans la méthode didSelectRowAtIndexPath. Ensuite, dans numberOfRowsInSection, donnez un nouveau nombre. Dans heightForRowAtIndexpath spécifiez les hauteurs personnalisées. Dans cellForRowAtIndexpath, ajoutez des cellules personnalisées.