Extension for the UITableView that allows a user to select the headerView of tableView and call back, can be used to make a foldable tableView.
- Include
UITableView+HeaderView
category andUITableViewDelegate_HeaderViewSelection
protocol. - UITableView's delegate should implement the
UITableViewDelegate_HeaderViewSelection
protocol. - When implementing the method
tableView:viewForHeaderInSection:
, call the category methodheaderView:forSection
using the returning headerView as the parameter.
It works because a tapGestureRecognizer is added to the headerView, so the headerView can send callback when selection occurs.
In the headerView selection callback method tableView:didSelectHeaderView:inSection:
and tableView:didSelectHeaderView:inSection:
, refresh the section in where the headerView lays, returning the corresponding number of row of the section, done!
It works like: