I have a UITableview in which each cell has a button. My issue is if i clicked the button in the first row, the height of the cell is increased, then i click the another button in the tableviewcell already expanded cell height will be reduced and selected cell height will be increased
After trying this link UITableView: How to change cell height dynamically when a button is clicked in it? Swift
here is my code:
var indexOfExpendedCell:NSInteger = -1
var shouldCellBeExpanded:Bool = false
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
cell.optionButton?.addTarget(self, action: #selector(ViewController.optionAction), forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if shouldCellBeExpanded && indexPath.row == indexOfExpendedCell {
return 102
}
else { return 57 }
}
}
func optionAction(sender: UIButton){
if let button = sender as? UIButton {
shouldCellBeExpanded = !shouldCellBeExpanded
indexOfExpendedCell = sender.tag
if shouldCellBeExpanded {
self.tableView.beginUpdates()
self.tableView.endUpdates()
button.setImage(UIImage(named: "Reject Icon filled"), forState: .Normal)
}
else {
self.tableView.beginUpdates()
self.tableView.endUpdates()
button.setImage(UIImage(named: "Reject Icon"), forState: .Normal)
}
}
}
after trying this code, the cell height is expanded and reduced when i click the button in the same row. I failed to do following steps
Steps : 1. click the second row the height of cell is expanded 2. now click the first row i need to reduce height of second row and expand the height of first row
Can anyone help me?