Instead of using UITableView or UICollectionView use this forexpand/collapse behaviour.Implementation higly mimics UICollectionView. Its more easy becausejust waits the headers and their expanding items which are any UIViewtype object -
View it on GitHub