Ajouter une propriété de garder une trace de la cellule sélectionnée
@property (nonatomic) int currentSelection;
Réglez - le à une valeur sentinelle (par exemple) viewDidLoad, pour vous assurer que les UITableViewdéparts en position « normale »
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//sentinel
self.currentSelection = -1;
}
En heightForRowAtIndexPathvous pouvez définir la hauteur souhaitée pour la cellule sélectionnée
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
int rowHeight;
if ([indexPath row] == self.currentSelection) {
rowHeight = self.newCellHeight;
} else rowHeight = 57.0f;
return rowHeight;
}
En didSelectRowAtIndexPathvous enregistrer la sélection en cours et enregistrer une hauteur dynamique, si nécessaire
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// do things with your cell here
// set selection
self.currentSelection = indexPath.row;
// save height for full text label
self.newCellHeight = cell.titleLbl.frame.size.height + cell.descriptionLbl.frame.size.height + 10;
// animate
[tableView beginUpdates];
[tableView endUpdates];
}
}
Dans didDeselectRowAtIndexPathrégler l'indice de sélection revenir à la valeur sentinelle et animer la cellule de retour à la forme normale
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
// do things with your cell here
// sentinel
self.currentSelection = -1;
// animate
[tableView beginUpdates];
[tableView endUpdates];
}
}