Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableViewCell expand on click

Lets say we have a custom UITableViewCell

So whenever I click custom button on cell.. it should expand to the some extent (you can say 40 height more...) and when i click again to the same custom button it should collapse to the previous height.

Developer's please guide me.. how can I achieve this task

like image 396
Tariq Avatar asked Jan 08 '11 18:01

Tariq


People also ask

How do you expand and contract height of a UITableView cell when tapped?

Animating the cell to expand and contract You supply an array of rows (indexpath) that needs to be reloaded, then the tableview will reload them by calling cellForRowAt: on these cell to layout them again, and animate the transition with the RowAnimation you passed into the with: parameter.


1 Answers

I'm not going to say anything here to contradict the accepted answer considering it is perfectly correct. However, I am going to go into more detail on how to accomplish this. If you don't want to read through all this and are more interested in playing with the source code in a working project, I've uploaded an example project to GitHub.

The basic idea is is to have a condition inside of the method -tableView: heightForRowAtIndexPath: that determines whether or not the current cell should be expanded. This will be triggered by calling begin/end updates on the table from within -tableView: didSelectRowAtIndexPath: In this example, I'll show how to make a table view that allows for one cell to be expanded at a time.

The first thing that you'll need to do is declare a reference to an NSIndexPath object. You can do this however you want, but I recommend using a property declaration like this:

@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 nil will only mean that the table will not initially have an expanded row. If you would rather the table start off with a row of your choice expanded, you could add something like this to your viewDidLoad method:

NSInteger row = 1; NSInteger section = 2; self.expandedIndexPath = [NSIndexPath indexPathForRow:row inSection:section]; 

The next step is to head on over to your UITableViewDelegate method -tableView: didSelectRowAtIndexPath: to add the logic to alter the expanded cell index based on the users selection. The idea here is to check the index path that has just been selected against the index path stored inside the expandedIndexPath variable. If the two are a match, then we know that the user is trying to deselect the expanded cell in which case, we set the variable to nil. Otherwise, we set the expandedIndexPath variable to the index that was just selected. This is all done between calls to beginUpdates/endUpdates, to allow the table view to automatically handle the transition animation.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {     [tableView beginUpdates]; // tell the table you're about to start making changes      // If the index path of the currently expanded cell is the same as the index that     // has just been tapped set the expanded index to nil so that there aren't any     // expanded cells, otherwise, set the expanded index to the index that has just     // been selected.     if ([indexPath compare:self.expandedIndexPath] == NSOrderedSame) {         self.expandedIndexPath = nil;     } else {         self.expandedIndexPath = indexPath;     }      [tableView endUpdates]; // tell the table you're done making your changes } 

Then the final step is in another UITableViewDelegate method -tableView: heightForRowAtIndexPath:. This method will be called after you've triggered beginUpdates once for each index path that the table determines needs updating. This is where you'll compare the expandedIndexPath against the index path that is currently being reevaluated.

If the two index paths are the same, then this is the cell that you wish to be expanded, otherwise it's 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 {     // Compares the index path for the current cell to the index path stored in the expanded     // index path variable. If the two match, return a height of 100 points, otherwise return     // a height of 44 points.     if ([indexPath compare:self.expandedIndexPath] == NSOrderedSame) {         return 100.0; // Expanded height     }     return 44.0; // Normal height } 
like image 123
Mick MacCallum Avatar answered Oct 19 '22 11:10

Mick MacCallum