我正在为我的 TableView 使用自定义 UITableViewHeaderFooterView。我试图在一个部分(我正在工作)中实现隐藏和显示行。我决定在部分标题中添加一个按钮 (>),以便在部分“展开/折叠”时可以旋转它。
单击按钮时会出现我遇到的问题。调用该rotateCollapseButton()
函数时,所有节标题中的 (>) 按钮都会旋转,而不仅仅是单击的那个。有时它甚至会排除被单击的按钮,或者单击一个按钮会影响另一个按钮而不是它本身。我怎样才能使它只有正确的按钮会旋转?
这是我创建的自定义 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
}
}
编辑:初始化标头的代码...
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
}
谢谢!