当collectionView折叠时隐藏collectionView内容

问题描述 投票:0回答:1

我有一个带有可扩展和可折叠部分的collectionView。当我折叠这些节时,表View内的内容仍然存在。我想知道是否有人知道如何改变这一点?我有一个UicollectionViewCell,可以在其中以编程方式创建视图。但是,我以为我的问题到了,因为我没有在cellForItemAt内部创建视图。我将在下面保留我的代码,请让我知道您的想法。

//这就是我创建sectionHeaders并注册collectionViewCell的方式。

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

    var reusableview = UICollectionReusableView()
    if (kind == UICollectionView.elementKindSectionHeader) {
        let section = indexPath.section
        switch (section) {
        case 0:
            let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerIdentifier, for: indexPath) as? userProfileHeader
                        if let user = self.user {
                                headerView?.myUser = user
                            } else if let userToLoad = self.userToLoad {
                                headerView?.myUser  = userToLoad
                            }
            reusableview = headerView!


        case 1:
            let sectionViews = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: skillsSection, for: indexPath) as? skillsprefSection
            sectionViews?.downArrowBtn.addTarget(self, action: #selector(showCells), for: .touchUpInside)
            sectionViews?.headerLabel.text = "Skills & Preferences"


            if expandedRow == false {
                sectionViews?.contentView.isHidden = true
            }

            reusableview = sectionViews!

        case 2:
            let bioSectionThing = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: bioSectionHeader, for: indexPath) as? bioSection
                bioSectionThing?.downArrowBtn.addTarget(self, action: #selector(showCells), for: .touchUpInside)
                bioSectionThing?.headerLabel.text = "Bio"
                reusableview = bioSectionThing!

        case 3:
            let reviewSectionThing = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: reviewSectionHeader, for: indexPath) as? reviewSection
                reviewSectionThing?.downArrowBtn.addTarget(self, action: #selector(showCells), for: .touchUpInside)
                reviewSectionThing?.headerLabel.text = "Reviews"
                reusableview = reviewSectionThing!

         case 4:
                  let recentSectionThing = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: recentSectionHeader, for: indexPath) as? recentSection
                    recentSectionThing?.downArrowBtn.addTarget(self, action: #selector(showCells), for: .touchUpInside)
                      recentSectionThing?.headerLabel.text = "Recent"
                      reusableview = recentSectionThing!
                default:
            return reusableview

        }
    }
    return reusableview

}

//这是我扩展/收缩部分的方式

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {

    if(section==0) {
        return .init(width: view.frame.width, height: 340)


    } else if (section==1) {
        if expandedRow == true {
               return .init(width: view.frame.width, height: 450)
           } else {
               return .init(width: view.frame.width, height: 133)
           }

    } else if (section==2) {
         if expandedRow == true {
        return .init(width: view.frame.width, height: 400)
        } else {
        return .init(width: view.frame.width, height: 133)
        }

    } else if (section==3) {
    if expandedRow == true {
    return .init(width: view.frame.width, height: 400)
    } else {
    return .init(width: view.frame.width, height: 133)
    }

    } else if (section==4) {
        if expandedRow == true {
        return .init(width: view.frame.width, height: 400)
        } else {
        return .init(width: view.frame.width, height: 133)
    }

    } else {
        return .init(width: view.frame.width, height: 100)
    }

}

这是折叠后图像的外观。this is how the image looks when sections are collapsed.

这是该部分在展开时的外观。

感谢您的所有帮助。我的目标是在折叠该节时隐藏与该节关联的所有内容。并在展开部分时显示。

this how the section looks when expanded.

ios xcode uicollectionview uicollectionviewcell
1个回答
0
投票

尝试下面的代码作为您自己的数据源的示例

class ViewController: UIViewController {
    private var dataSource = [MenuItems]()
    var tableView: UITableView = {
        let tableView = UITableView()
        tableView.translatesAutoresizingMaskIntoConstraints = false
        return tableView
    }()
    override func viewDidLoad() {
        super.viewDidLoad()
        configureView()
        loadJSONBundle()    }
    private func loadJSONBundle(){
        if let path = Bundle.main.path(forResource: "AllMenu", ofType: "json") {
            do {
                let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
                if  let allMenu = try? JSONDecoder().decode([MenuItems].self, from: data){
                    self.dataSource.append(contentsOf: allMenu)
                }else{
                    print("parsing failed")
                }
            } catch {
            }
        }
    }
    func configureView(){
            self.view.addSubview(self.tableView)
            tableView.delegate = self
            tableView.dataSource = self
            tableView.backgroundColor = #colorLiteral(red: 0.8039215803, green: 0.8039215803, blue: 0.8039215803, alpha: 1)
            tableView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
            tableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true
            tableView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
            tableView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
            tableView.register(CategoryTableViewCell.nib(), forHeaderFooterViewReuseIdentifier: CategoryTableViewCell.nameOfClass)
            tableView.register(SubCategoryTableViewCell.nib(), forCellReuseIdentifier: SubCategoryTableViewCell.nameOfClass)
            tableView.separatorColor = .white
            tableView.separatorStyle = .none
        }
    func expandTableView(_ position:Int){
            if dataSource[position].open{
                dataSource[position].open = false
            }else{
                dataSource[position].open = true
            }
                self.tableView.reloadSections(NSIndexSet(index:position) as IndexSet, with: UITableView.RowAnimation.automatic)
    }
}
    extension ViewController:UITableViewDelegate{
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return UITableView.automaticDimension
        }
        func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
            return 68
        }
        func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
            let headerView = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: CategoryTableViewCell.nameOfClass) as! CategoryTableViewCell
            headerView.headerView.tag = section
            headerView.config(dataSource[section])
            headerView.onclickHandler = {[weak self] (position) in
                self?.expandTableView(position)
            }
            return headerView
        }
    }
    extension ViewController:UITableViewDataSource{
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            if dataSource[section].open {
                return dataSource[section].subCategory.count
            }else{
                 return 0
            }
        }
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = self.tableView.dequeueReusableCell(withIdentifier: SubCategoryTableViewCell.nameOfClass, for: indexPath) as! SubCategoryTableViewCell
            cell.config(dataSource[indexPath.section].subCategory[indexPath.row])
            return cell
        }
        func numberOfSections(in tableView: UITableView) -> Int {
            return dataSource.count
        }
}

© www.soinside.com 2019 - 2024. All rights reserved.