繁体   English   中英

单击CollectionView单元格时如何更新表视图数据(tableview.reloadData()在CollectionView的didSelectItemAt内部不起作用)

[英]How to update table view data when a CollectionView Cell is clicked ( tableview.reloadData() not working inside CollectionView's didSelectItemAt )

单击链接以查看图像。 我在同一视图控制器中有一个集合视图和一个表视图。 我想基于集合视图中的选定项目更新表视图的数据。 因此,每当我单击集合视图的任何项目时,我的表视图数据都将更新。 我的代码如下:

public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        for superSkill in skills!{
            if superSkill.name == (skills?[indexPath.row].name)! {
                childSkills = superSkill.skills!
            }
        }

        DispatchQueue.main.async{
            self.childSkillTableView.reloadData()
        }

    }

有什么办法可以实现

首先要确保您的childSkillTableView.dataSource = self和您的superSkillsCollectionView.delegate = self

第二件事,没有理由使用DispatchQueue.main.async({})

第三件事,虽然不太重要,但是可以使用类似以下内容的方法来代替for循环:

childSkills = skills?.first(where { superSkill in superSkill.name == (skills?[indexPath.row].name)! }

尽管您应该使用一些if letguard let语句来检查可选内容,而不是强制展开。

extension PerformanceVC: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return skills.count
    }


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {


        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "superSkillCell", for: indexPath) as! SuperSkillCell

        cell.superSkillName.text = skills[indexPath.row].name
        //loading image async
        ImageAsyncLoader.loadImageAsync(url: (skills[indexPath.row].imageURL)!, imgView: cell.superSkillImage)

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
    {
        return CGSize(width: skillCollections.frame.height * 0.9, height: skillCollections.frame.height) //use height whatever you wants.
    }

    public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        print((skills[indexPath.row].name)!)

        for skill in skills{
            if skill.name == (skills[indexPath.row].name)! {
                childSkills = skill.skills!
            }
        }

        self.subSkillTableView.reloadData()
    }

}

extension PerformanceVC: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let openViewHeight: Int = 55

        if (indexPath.row == selectedRowIndex.row && isExpanded == false){
            isExpanded = true
            return CGFloat(openViewHeight + 36 * (childSkills[indexPath.row].skills?.count)!)

        } else if (indexPath.row == selectedRowIndex.row && isExpanded == true){
            isExpanded = false
            return CGFloat(openViewHeight)
        } else {
            isExpanded = false
            return CGFloat(openViewHeight)
        }


    }

    public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return childSkills.count
    }


    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        print(childSkills[indexPath.row].name)
        selectedRowIndex = indexPath
        tableView.beginUpdates()

        //let cell = tableView.dequeueReusableCell(withIdentifier: "SubSkillTableCell", for: indexPath) as! SubSkillTableCell
        let cell = tableView.cellForRow(at: indexPath) as! SubSkillTableCell

        for subview in cell.grandSkillStack.subviews {
            subview.removeFromSuperview()
        }

        var grandSkillView: GrandChildSkillItem
        grandChildSkills = (childSkills[indexPath.row].skills)!
        for grandchildskill in grandChildSkills {
            grandSkillView = GrandChildSkillItem(frame: CGRect(x: 0, y: 0, width: 300, height: 30))
            grandSkillView.grandChildSkillNameLabel.text = grandchildskill.name
            grandSkillView.grandChildSkillProgress.progress = Float(grandchildskill.percentage!)

            cell.grandSkillStack.addArrangedSubview(grandSkillView)
        }

        tableView.endUpdates()

    }

    public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false

        let cell = tableView.dequeueReusableCell(withIdentifier: "SubSkillTableCell", for: indexPath) as! SubSkillTableCell
        cell.subSkillName.text = childSkills[indexPath.row].name
        cell.subSkillProgress.progress = Float(childSkills[indexPath.row].percentage!)

        if let uPoints = childSkills[indexPath.row].userPoints  {
            if let tPoints = childSkills[indexPath.row].totalPoints {
                if let count = childSkills[indexPath.row].skills?.count {
                    cell.subSkillDetail.text = "\(uPoints)" + "/" + "\(tPoints)" + "XP \u{2022} " + "\(count)" + " subskills"
                }

            }
        }

        return cell
    }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM