Expand and Collapse tableview cells Expand and Collapse tableview cells ios ios

Expand and Collapse tableview cells


If you want the cell to get physically bigger, then where you have your store IndexPath, in heightForRow: use:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {    if selectedIndexPath == indexPath {      return 230 + extraHeight    }    return 230.0}

Then when you want to expand one in the didSelectRow:

selectedIndexPath = indexPathtableView.beginUpdatestableView.endUpdates

Edit

This will make the cells animate themselves getting bigger, you dont need the extra animation blocks in the cell.

Edit 2

 override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {        if(selectedIndexPath == indexPath) {          selectedIndexPath = nil          if let cell = tableView.cellForRowAtIndexPath(indexPath) as? MyTicketsTableViewCell {            cell.collapse()          }          if let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow:indexPath.row+1, section: indexPath.section) as? MyTicketsTableViewCell {            cell.collapse()          }        } else {          selectedIndexPath = indexPath          if let cell = tableView.cellForRowAtIndexPath(indexPath) as? MyTicketsTableViewCell {              cell.expand()          }          if let cell = tableView.cellForRowAtIndexPath(NSIndexPath(forRow:indexPath.row+1, section: indexPath.section) as? MyTicketsTableViewCell {             cell.expand()          }        }        tableView.beginUpdates()        tableView.endUpdates()    }


All you need is implement UITableView Delegate this way:

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {    return UITableViewAutomaticDimension}override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {    return UITableViewAutomaticDimension}

By default, estimatedHeight is CGRectZero, when you set some value for it, it enables autoresizing (the same situation with UICollectionView), you can do even also so:

tableView?.estimatedRowHeight = CGSizeMake(50.f, 50.f); 

Then you need to setup you constraints inside your cell.

Check this post: https://www.hackingwithswift.com/read/32/2/automatically-resizing-uitableviewcells-with-dynamic-type-and-ns

Hope it helps)


In MyTicketsTableViewController class, inside cellForRowAtIndexPath datasource method add target for the button.

cell.expandButton.addTarget(self, action: "expandorcollapsed:", forControlEvents: UIControlEvents.TouchUpInside)