影响所有 TableView 中按钮的操作 Headers

Action affecting buttons in all TableView Headers

我正在为我的 TableView 使用自定义 UITableViewHeaderFooterView。我试图在一个部分(我正在工作)中实现隐藏和显示行。我决定在 header 部分添加一个按钮 (>),这样我就可以在 "expanded/collapsed" 部分旋转它。

我点击按钮时出现的问题。调用 rotateCollapseButton() 函数时,所有 header 部分中的 (>) 按钮都会旋转,而不仅仅是被单击的按钮。有时它甚至会排除被点击的按钮,或者点击一个按钮会影响另一个按钮而不是它本身。 我怎样才能做到只有正确的按钮才会旋转?

这是我创建的自定义 Header 的代码。

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
    }
}

编辑:初始化 header 的代码...

 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
}

谢谢!

在您的页眉设置中,尝试这样做:

 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
}