I am not going to say anything to contradict the accepted answer, given that this is perfectly correct. However, I will tell you more about how to do this. If you donβt want to read all this and are more interested in playing with the source code in a working draft, I uploaded the project to GitHub .
The main idea is to have a condition inside the -tableView: heightForRowAtIndexPath: method, which determines whether the current cell should be expanded. This will be caused by invoking start / end updates in the table from -tableView: didSelectRowAtIndexPath: In this example, I will show how to create a table view that allows you to expand one cell at a time.
The first thing you need to do is declare a reference to the NSIndexPath object. You can do it however you want, but I recommend using a property declaration as follows:
@property (strong, nonatomic) NSIndexPath *expandedIndexPath;
NOTE. You do not need to create this index path inside viewDidLoad or any other similar method. The fact that the index is initially zero will mean that initially the table will not have an extended row. If you prefer the table to start with an extended row, you could add something like this to the viewDidLoad method:
NSInteger row = 1; NSInteger section = 2; self.expandedIndexPath = [NSIndexPath indexPathForRow:row inSection:section];
The next step is to switch to the UITableViewDelegate method -tableView: didSelectRowAtIndexPath: to add logic to change the extended index of the cell based on user selection. The idea here is to check the just selected pointer path against the index path stored inside the expandedIndexPath variable. If they match, then we know that the user is trying to cancel the selected cell, in this case we set the variable to nil. Otherwise, we set the expandedIndexPath variable to the index just selected. All of this is done between calls to beginUpdates / endUpdates to allow the table view to automatically handle transient animations.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [tableView beginUpdates];
Then the last step is in another UITableViewDelegate -tableView: heightForRowAtIndexPath: method. This method is called after you beginUpdates once for each index path that the table determines needs to be updated. Here you compare the value of expandedIndexPath with the index that is currently being reevaluated.
If the two index paths are the same, then this is the cell you want to expand, otherwise the height should be normal. I used the values ββ100 and 44, but you can use what ever suits your needs.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {