0

I am using a custom UITableViewHeaderFooterView for me TableView. I was trying to implement hiding and showing rows in a section(which I have working). I decided to add a button (>) to the section header so that I can rotate it when the section is "expanded/collapsed".

The problem I have appears when I click the button. When the rotateCollapseButton() function is called, the (>) buttons in all the section headers rotate, not just the one that was clicked. Sometimes it'll even exclude the button that was clicked or clicking one will affect a different one and not itself. How can I make it so that only the correct button will rotate?

This is the code I have for the custom Header I created.

var rotated:Bool = false

var section:Int?

weak var delegate:MessageGroupHeaderDelegate?

@IBAction func expandCollapseButtonClicked(_ sender: Any) {
    rotateCollapseButton(sender as! UIButton)
    delegate?.didPressExpandCollapseButton(atSection : self.section!)
}

func rotateCollapseButton(_ button:UIButton) {
    UIView.animate(withDuration: 0.5) { () -> Void in
        var rotationAngle:CGFloat = CGFloat(M_PI_2)

        if self.rotated {
            rotationAngle = CGFloat(0)
        }

        button.transform = CGAffineTransform(rotationAngle : rotationAngle)

        self.rotated = !self.rotated
    }
}

EDIT: Code where the header is initialized...

 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    // Dequeue with the reuse identifier
    let cell = self.massMessageGroupsTableView.dequeueReusableHeaderFooterView(withIdentifier: "MessageGroupTableViewHeader")
    let header = cell as! MessageGroupTableViewHeader
    header.groupNameLabel.text = messageGroupsMap[section]?.messageGroup.name
    header.section = section
    header.setComposeButtonImage()
    header.delegate = self

    return cell
}

Thank you!

1 Answers1

0

In your header setting, trying doing this instead:

 func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    // Dequeue with the reuse identifier
    let cell = self.massMessageGroupsTableView.dequeueReusableCell(withIdentifier: "MessageGroupTableViewHeader")
    let header = cell as! MessageGroupTableViewHeader
    header.groupNameLabel.text = messageGroupsMap[section]?.messageGroup.name
    header.section = section
    header.setComposeButtonImage()
    header.delegate = self

    let containingView : UIView = UIView()
    containingView.addSubview(header)

    return containingView
}
unkgd
  • 671
  • 3
  • 12
  • I tried this but I can't see any of my buttons. I'm guessing its an autolayout issue. Once I figure that out I'll report back. Thanks! – Brian Benavides Feb 13 '17 at 17:14
  • Not sure if it matters or not but `MessageGroupTableViewHeader ` subclasses `UITableViewHeaderFooterView`. I have a separate .xib for it. This is why I used `self.massMessageGroupsTableView.dequeueReusableHeaderFooterView(withIdentifier: "MessageGroupTableViewHeader")` – Brian Benavides Feb 13 '17 at 17:26
  • Try and subclass UITableViewCell instead of HeaderFooterView – unkgd Feb 13 '17 at 17:39
  • Even though my intent is to have custom section header? – Brian Benavides Feb 13 '17 at 17:43
  • use the tablecell as the custom section, this is why there is a conversion to a UIView at the end, to prevent it from moving when editing the table – unkgd Feb 13 '17 at 17:45
  • I changed everything to subclass UITableViewCell instead but now the button does not rotate at all(neither do the rest). It still functions as I intend save for the rotating button. – Brian Benavides Feb 13 '17 at 17:55
  • Because these are cells now, you should keep the rotation state of every section the the controller, same as you keep an array of items per section, keep the rotation and any other data, and when the "headerForSection" is called, update the rotation state. Regarding the animation, is your button action getting called? – unkgd Feb 13 '17 at 19:29
  • The action is being called. The action is performed. However, even though I perform the action on the `sender` it affects all buttons that are displayed. – Brian Benavides Feb 14 '17 at 19:09