点击tableViewHeader控制cell关闭展开以及label的代码自定义约束

来源:互联网 发布:淘宝访客计算方式 编辑:程序博客网 时间:2024/06/16 07:07

// Arrow label

        contentView.addSubview(arrowLabel)

        arrowLabel.textColor =UIColor.white

        arrowLabel.translatesAutoresizingMaskIntoConstraints =false

        arrowLabel.widthAnchor.constraint(equalToConstant:12).isActive =true

        arrowLabel.topAnchor.constraint(equalTo: marginGuide.topAnchor).isActive = true

        arrowLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor).isActive = true

        arrowLabel.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor).isActive = true

//

        // Call tapHeader when tapping on this header

        //

        addGestureRecognizer(UITapGestureRecognizer(target:self, action: #selector(CollapsibleTableViewHeader.tapHeader(_:))))



完整代码:

import UIKit


protocol CollapsibleTableViewHeaderDelegate {

    func toggleSection(_ header:CollapsibleTableViewHeader, section:Int)

}


class CollapsibleTableViewHeader:UITableViewHeaderFooterView {

    

    var delegate:CollapsibleTableViewHeaderDelegate?

    var section:Int = 0

    

    let titleLabel =UILabel()

    let arrowLabel =UILabel()

    

    overrideinit(reuseIdentifier: String?) {

        super.init(reuseIdentifier: reuseIdentifier)

        

        // Content View

        contentView.backgroundColor =UIColor(hex:0x2E3944)

        

        let marginGuide =contentView.layoutMarginsGuide

        

        // Arrow label

        contentView.addSubview(arrowLabel)

        arrowLabel.textColor =UIColor.white

        arrowLabel.translatesAutoresizingMaskIntoConstraints =false

        arrowLabel.widthAnchor.constraint(equalToConstant:12).isActive =true

        arrowLabel.topAnchor.constraint(equalTo: marginGuide.topAnchor).isActive = true

        arrowLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor).isActive = true

        arrowLabel.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor).isActive = true

        

        // Title label

        contentView.addSubview(titleLabel)

        titleLabel.textColor =UIColor.white

        titleLabel.translatesAutoresizingMaskIntoConstraints =false

        titleLabel.topAnchor.constraint(equalTo: marginGuide.topAnchor).isActive = true

        titleLabel.trailingAnchor.constraint(equalTo: marginGuide.trailingAnchor).isActive = true

        titleLabel.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor).isActive = true

        titleLabel.leadingAnchor.constraint(equalTo: marginGuide.leadingAnchor).isActive = true

        

        //

        // Call tapHeader when tapping on this header

        //

        addGestureRecognizer(UITapGestureRecognizer(target:self, action: #selector(CollapsibleTableViewHeader.tapHeader(_:))))

        

    }

    

    requiredinit?(coder aDecoder: NSCoder) {

        fatalError("init(coder:) has not been implemented")

    }

    

    //

    // Trigger toggle section when tapping on the header

    //

    func tapHeader(_ gestureRecognizer:UITapGestureRecognizer) {

        guardlet cell = gestureRecognizer.viewas? CollapsibleTableViewHeaderelse {

            return

        }

        

        delegate?.toggleSection(self, section: cell.section)

    }

    

    func setCollapsed(_ collapsed:Bool) {

        //

        // Animate the arrow rotation (see Extensions.swf)

        //

        arrowLabel.rotate(collapsed ?0.0 : .pi /2)

    }

    

}

extension UIView {


    func rotate(_ toValue:CGFloat, duration: CFTimeInterval = 0.2) {

        let animation =CABasicAnimation(keyPath:"transform.rotation")

        

        animation.toValue = toValue

        animation.duration = duration

        animation.isRemovedOnCompletion =false

        animation.fillMode =kCAFillModeForwards

        

        self.layer.add(animation, forKey:nil)

    }


}




import UIKit


//

// MARK: - View Controller

//

class CollapsibleTableViewController:UITableViewController {

    

    var sections =sectionsData

    

    overridefunc viewDidLoad() {

        super.viewDidLoad()

        

        // Auto resizing the height of the cell

        tableView.estimatedRowHeight =44.0

        tableView.rowHeight =UITableViewAutomaticDimension

        

        self.title ="Apple Products"

    }

    

}


//

// MARK: - View Controller DataSource and Delegate

//

extension CollapsibleTableViewController {


    overridefunc numberOfSections(in tableView:UITableView) -> Int {

        returnsections.count

    }

    

    overridefunc tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int {

        returnsections[section].collapsed ?0 : sections[section].items.count

    }

    

    // Cell

    overridefunc tableView(_ tableView:UITableView, cellForRowAt indexPath:IndexPath) -> UITableViewCell {

        let cell:CollapsibleTableViewCell = tableView.dequeueReusableCell(withIdentifier:"cell") as?CollapsibleTableViewCell ??

            CollapsibleTableViewCell(style: .default, reuseIdentifier:"cell")

        

        let item:Item = sections[indexPath.section].items[indexPath.row]

        

        cell.nameLabel.text = item.name

        cell.detailLabel.text = item.detail

        

        return cell

    }

    

    overridefunc tableView(_ tableView:UITableView, heightForRowAt indexPath:IndexPath) -> CGFloat {

        returnUITableViewAutomaticDimension

    }

    

    // Header

    overridefunc tableView(_ tableView:UITableView, viewForHeaderInSection section:Int) -> UIView? {

        let header = tableView.dequeueReusableHeaderFooterView(withIdentifier:"header") as?CollapsibleTableViewHeader ??CollapsibleTableViewHeader(reuseIdentifier:"header")

        

        header.titleLabel.text =sections[section].name

        header.arrowLabel.text =">"

        header.setCollapsed(sections[section].collapsed)

        

        header.section = section

        header.delegate =self

        

        return header

    }

    

    overridefunc tableView(_ tableView:UITableView, heightForHeaderInSection section:Int) -> CGFloat {

        return44.0

    }

    

    overridefunc tableView(_ tableView:UITableView, heightForFooterInSection section:Int) -> CGFloat {

        return1.0

    }


}


//

// MARK: - Section Header Delegate

//

extension CollapsibleTableViewController:CollapsibleTableViewHeaderDelegate {

    

    func toggleSection(_ header:CollapsibleTableViewHeader, section:Int) {

        let collapsed =!sections[section].collapsed

        

        // Toggle collapse

        sections[section].collapsed = collapsed

        header.setCollapsed(collapsed)

        

        tableView.reloadSections(NSIndexSet(index: section)as IndexSet, with: .automatic)

    }

    

}



原创粉丝点击