影响所有TableView标题中按钮的操作

问题描述:

我为我的TableView使用自定义的UITableViewHeaderFooterView。我试图隐藏和显示行中的行(我正在工作)。我决定在节标题中添加一个按钮(>),以便在节“展开/折叠”时旋转它。影响所有TableView标题中按钮的操作

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

这是我为自定义标题创建的代码。

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 
} 

谢谢!

+0

你能也粘贴你如何使用节头中的代码?因为如果你使用相同的对象,它可能是所有的旋转的原因 – unkgd

+0

我上面添加了一个编辑。那是你所指的? –

+0

是的,你所有的部分都出现在屏幕上吗?或者你看到他们在滚动后旋转了箭头? – unkgd

在你的头的设置,试图这样做,而不是:

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

我试过这个,但是我看不到我的任何按钮。我猜它是一个自动布局问题。一旦我知道了,我会报告回来。谢谢! –

+0

不确定它是否重要,但是'MessageGroupTableViewHeader'子类'UITableViewHeaderFooterView'。我有一个单独的.xib。这就是为什么我使用'self.massMessageGroupsTableView.dequeueReusableHeaderFooterView(withIdentifier:“MessageGroupTableViewHeader”)' –

+0

尝试和继承UITableViewCell而不是HeaderFooterView – unkgd