2017-02-13 8 views
0

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

EDIT:ヘッダが初期化されたコード...

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

答えて

0

、しようと、この代わりにやって:

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

HeaderFooterViewではなくUITableViewCellを試してサブクラス化してください。 – unkgd

関連する問題