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
I have created an open source library for this. You just implement collapse and expand delegates in your code and voilà! you can also perform any drawings and animations. check out this.
Following this medium article on how to expand the cells based on the tap of a button and setting the
numbersOfLine
for a specific label, I was able to perform the animation usingNotice
performBatchUpdates
is only available in iOS 11⬆️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:
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:
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 theexpandedIndexPath
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 theexpandedIndexPath
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.Then the final step is in another UITableViewDelegate method
-tableView: heightForRowAtIndexPath:
. This method will be called after you've triggeredbeginUpdates
once for each index path that the table determines needs updating. This is where you'll compare theexpandedIndexPath
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.
Implement heightForRowAtIndexPath to calculate the right height. Then in the code for your button, force the table to reevaluate each cell's height with beginUpdates plus endUpdates:
Changes to the tableview cells' heights will automatically be calculated with heightForRowAtIndexPath and the changes will be animated too.
In fact, instead of a button on your cell that does this, you might even just make selecting the cell do this in
didSelectRowAtIndexPath
.This is Mick's answer but for Swift 4. (IndexPath replaces NSIndexPath, which comes with an empty IndexPath as nil would crash Swift. Also, you can compare two instances of IndexPath using
==
)Declare the expandedIndexPath property.
Optional viewDidLoad part.
Then the didSelectRow part.
Then the heightForRow part.
I've made a reusable component that will do exactly what you're talking about. It's pretty easy to use, and there's a demo project.
GCRetractableSectionController on GitHub.