Expand/collapse section in UITableView in iOS

前端 未结 17 929
野的像风
野的像风 2020-11-22 08:49

Could somebody tell me the way to perform UITableView expandable/collapsible animations in sections of UITableView as below?

<

17条回答
  •  没有蜡笔的小新
    2020-11-22 09:32

    I have done the same thing using multiple sections .

    class SCTierBenefitsViewController: UIViewController {
        @IBOutlet private weak var tblTierBenefits: UITableView!
        private var selectedIndexPath: IndexPath?
        private var isSelected:Bool = false
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            tblTierBenefits.register(UINib(nibName:"TierBenefitsTableViewCell", bundle: nil), forCellReuseIdentifier:"TierBenefitsTableViewCell")
            tblTierBenefits.register(UINib(nibName:"TierBenefitsDetailsCell", bundle: nil), forCellReuseIdentifier:"TierBenefitsDetailsCell")
    
            tblTierBenefits.rowHeight = UITableViewAutomaticDimension;
            tblTierBenefits.estimatedRowHeight = 44.0;
            tblTierBenefits.tableFooterView = UIView()
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        }
    
    }
    
    extension SCTierBenefitsViewController : UITableViewDataSource{
    
        func numberOfSections(in tableView: UITableView) -> Int {
            return 7
        }
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return (isSelected && section == selectedIndexPath?.section) ? 2 : 1 
        }
    
        func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            return  0.01
        }
    
        func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            return nil
        }
    
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            switch indexPath.row {
            case 0:
                let cell:TierBenefitsTableViewCell = tableView.dequeueReusableCell(withIdentifier: "TierBenefitsTableViewCell")! as! TierBenefitsTableViewCell
                cell.selectionStyle = .none
                cell.contentView.setNeedsLayout()
                cell.contentView.layoutIfNeeded()
                return cell
    
            case 1:
                let cell:TierBenefitsDetailsCell = tableView.dequeueReusableCell(withIdentifier: "TierBenefitsDetailsCell")! as! TierBenefitsDetailsCell
                cell.selectionStyle = .none
                return cell
    
            default:
                break
            }
    
            return UITableViewCell()
        }
    }
    
    extension SCTierBenefitsViewController : UITableViewDelegate{
    
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            if indexPath.row == 0 {
    
                if let _selectedIndexPath = selectedIndexPath ,selectedIndexPath?.section == indexPath.section {
                    tblTierBenefits.beginUpdates()
                    expandCollapse(indexPath: _selectedIndexPath, isExpand: false)
                    selectedIndexPath = nil
                }
                else{
                    tblTierBenefits.beginUpdates()
                    if selectedIndexPath != nil {
                        tblTierBenefits.reloadSections([(selectedIndexPath?.section)!], with: .none)
                    }
                    expandCollapse(indexPath: indexPath, isExpand: true)
                }
            }
        }
    
        private func  expandCollapse(indexPath: IndexPath?,isExpand: Bool){
            isSelected = isExpand
            selectedIndexPath = indexPath
            tblTierBenefits.reloadSections([(indexPath?.section)!], with: .none)
            tblTierBenefits.endUpdates()
        }
    
    }
    

提交回复
热议问题