6

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?

Community
  • 1
  • 1
Aravind
  • 1,080
  • 2
  • 12
  • 18
  • I think you seem to need to have two array variables that store the information of your cell's expandable status and expanded index. So you can make use of those arrays to keep track of cell's status in either your optionAction function or tableview delegate function. – woogii Sep 24 '16 at 07:22
  • @woogii Thanks your reply. – Aravind Sep 26 '16 at 06:26

1 Answers1

9

Create an array to hold which cells should be expanded. When a button is pressed, add or remove an element to the array accordingly. Here is the code:

// Initialize an empty array of integers
var expandedCells = [Int]()

@IBAction func buttonPressed(_ sender: AnyObject) {
    // If the array contains the button that was pressed, then remove that button from the array
    if expandedCells.contains(sender.tag) {
        expandedCells = expandedCells.filter({ $0 != sender.tag})
    }
    // Otherwise, add the button to the array
    else {
        expandedCells.append(sender.tag)
    }
    // Reload the tableView data anytime a button is pressed
    tableView.reloadData()
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TableViewCell
    // Set the tag on your button to be equal to indexPath.row
    // This allows @IBAction func buttonPressed(_ sender: AnyObject) above to discern which row was tapped
    cell.myButton.tag = indexPath.row
    return cell
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    // Set the row height based on whether or not the Int associated with that row is contained in the expandedCells array
    if expandedCells.contains(indexPath.row) {
        return 102
    } else {
        return 57
    }
}
Austin Wood
  • 368
  • 1
  • 4
  • 14
  • Sorry, still i have the same issue. I click the second cell it doesnt reduce already expanded cell height – Aravind Sep 26 '16 at 06:07
  • Thanks, I removed all values in the array before adding. Now it works fine – Aravind Sep 26 '16 at 06:25
  • one more change in the above code. we can use tableView.beginUpdates(), tableView.endUpdates()` instead of tablview.reloaddata(). – Aravind Sep 26 '16 at 12:24
  • it does not work when number of rows are more (need scrolling) like 10 or more ... could u plz explain how to resolve this?? – iAj Aug 17 '17 at 10:33
  • @iajmeri43 Can you elaborate on what is not working? What happens exactly when you have more rows than can fit on the screen? – Austin Wood Aug 18 '17 at 20:04
  • @AustinWood tnx a lot for reply.. I got the solution from my colleagues yesterday... – iAj Aug 19 '17 at 04:43
  • @iajmeri43 Great, glad to hear. Do you want to either propose an edit to my answer, or add your own answer so that others may benefit from your solution? – Austin Wood Aug 20 '17 at 20:41
  • how to collapse the cell after expanded? – Fido May 21 '18 at 08:16