Tarvo Mäesepp Tarvo Mäesepp - 3 months ago 6
Swift Question

Tableview not going bigger if expanding cell

I've implemented the thing where you click on cell or button and the

tableview
cell
will expand. But the problem is if I expand the last
cell
it is behind the
tableview
. How can I make it to resize the
tableview
also and move the cell up a little?

I made it simply with constraints and
views
inside cell.

It looks like this, the blue one is main one and black is going to expand.

enter image description here

I don't know it the code is necessray but this is how I make it to expand.

This is inside
cellForRowAtIndexPath
:

if (self.selectedIndex == indexPath.row){
self.selectedIndex = -1
}else{
self.selectedIndex = indexPath.row
}

self.productstable.beginUpdates()
self.productstable.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
self.productstable.endUpdates()
}
return cell


This is inside
heightForRowAtIndexPath
:

if(selectedIndex == indexPath.row){
return 210
}else{
return 135
}


I tried to detect the last
cell
and then move it up but it is not working.

Answer

check this code

func reloadChange()
{
     let indexPath = NSIndexPath(forRow: selectedIndex, inSection: 0)
     self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
     //this is the way to scroll to your last cell
     self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true)
}


func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
     //code to expand and shrink your cell
     if(self.selectedIndex == indexPath.row)
      {
      self.selectedIndex = -1
      self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)
     //this is the way to scroll to your last cell
     self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true)
        return
      }
    self.selectedIndex = indexPath.row
    self.reloadChange()
}

I hope this helps you

Comments