【问题标题】:Hide collectionView content when collectionView is collapsed当collectionView折叠时隐藏collectionView内容
【发布时间】:2020-01-28 05:19:12
【问题描述】:

我有一个带有可展开和可折叠部分的 collectionView。当我折叠这些部分时,表格视图中的内容仍然存在。我想知道是否有人知道如何改变这个?我有一个 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)
    }

}

这是图片折叠时的样子。

这就是该部分在展开时的样子。

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

【问题讨论】:

  • cell.contentView.clipsToBounds = true & cell.clipsToBounds = true
  • @HarshalValanda 希望您将此作为答案发布,这有助于我完成所需的工作。
  • ????????好的发布@zach

标签: ios xcode uicollectionview uicollectionviewcell


【解决方案1】:

您的集合视图单元格超出范围,设置如下

cell.contentView.clipsToBounds = true
cell.clipsToBounds = true

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 感谢您的宝贵时间。我找到了答案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    • 1970-01-01
    • 2022-07-10
    • 2019-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多