可折叠标题动画不动画

时间:2018-02-16 01:32:41

标签: swift uitableview

您好我已经在我的swift中实现了一个可折叠的标题,标题效果很好,我能够折叠并解开该部分。问题是,当我使用新信息重新加载tableView时,标题中的箭头会丢失其动画,并且视图中的箭头在加载时不会旋转。如果您有任何想法,请告诉我。

代码:

tableView -

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: CollapsableHeader.CollapsableTableHeaderIdentifier) as? CollapsableHeader {
        header.setCollapsed(false)
        header.delegate = self

        return header
    }
}

//加载信息后我正在调用

    self.tableView.reloadData()

CollapsableHeader -

//其他方法

override func prepareForReuse() {
        super.prepareForReuse()
    }


    //Collapsed options
    func setCollapsed(_ collapsed: Bool) {
        //
        // Animate the arrow rotation (see Extensions.swf)
        //
        self.arrow.rotate(collapsed ? 0.0 : .pi / 2)
    }

1 个答案:

答案 0 :(得分:1)

尝试检查

项目制作的样本

链接 - https://github.com/RockinGarg/Expandable-TableView-

//Setting Header Customised View
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        //Declare cell
        let headerCell = tableView.dequeueReusableCell(withIdentifier: "HeaderCell") as! TableViewCell

        //Setting Header Components
        headerCell.titleLabel.text = self.section[section]
        headerCell.ButtonToShowHide.tag = section

        //Handling Button Title
        if self.collapaseHandlerArray.contains(self.section[section]){
            //if its opened
            headerCell.ButtonToShowHide.setTitle("Hide", for: .normal)
        }
        else{
            //if closed
            headerCell.ButtonToShowHide.setTitle("Show", for: .normal)
        }

        //Adding a target to button
        headerCell.ButtonToShowHide.addTarget(self, action: #selector(ViewController.HandleheaderButton(sender:)), for: .touchUpInside)
        return headerCell.contentView

    }

按钮处理程序

//Header cell button Action
    @objc func HandleheaderButton(sender: UIButton){

        //check status of button
        if let buttonTitle = sender.title(for: .normal) {
            if buttonTitle == "Show"{
                //if yes
                self.collapaseHandlerArray.append(self.section[sender.tag])
                sender.setTitle("Hide", for: .normal)
            }
            else {
                //if no
                while self.collapaseHandlerArray.contains(self.section[sender.tag]){
                    if let itemToRemoveIndex = self.collapaseHandlerArray.index(of: self.section[sender.tag]) {
                        //remove title of header from array
                        self.collapaseHandlerArray.remove(at: itemToRemoveIndex)
                        sender.setTitle("Show", for: .normal)
                    }
                }
            }
        }
        //reload section
        self.mainTableView.reloadSections(IndexSet(integer: sender.tag), with: .none)
    }

由于

相关问题