简体   繁体   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 )

click on the link to see the image. 单击链接以查看图像。 I have a collection view and a table view in the same view controller. 我在同一视图控制器中有一个集合视图和一个表视图。 I want to update the data of my table view, based on the selected item in the collection view. 我想基于集合视图中的选定项目更新表视图的数据。 So every time i click any item of collection view, my table view data should update. 因此,每当我单击集合视图的任何项目时,我的表视图数据都将更新。 My code is as follows: 我的代码如下:

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

    }

Is there any way i can achieve it 有什么办法可以实现

First thing is to make sure your childSkillTableView.dataSource = self and your superSkillsCollectionView.delegate = self 首先要确保您的childSkillTableView.dataSource = self和您的superSkillsCollectionView.delegate = self

The second thing, there's no reason to use DispatchQueue.main.async({}) 第二件事,没有理由使用DispatchQueue.main.async({})

The third thing, though less important, instead of a for loop, you might use something like: 第三件事,虽然不太重要,但是可以使用类似以下内容的方法来代替for循环:

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

Though you should use some if let or guard let statements to check for optionals instead of force unwrapping. 尽管您应该使用一些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.

相关问题 如何从位于 tableView 单元格内的 collectionView 的 didSelectItemAt 导航到 viewController? - How can I Navigate to a viewController from a collectionView's didSelectItemAt which is inside a tableView cell? 单击tableview header按钮时,如何将tableviewcell内的collectionview中的数据显示到另一个视图controller - How to display data from collectionview inside tableviewcell to another view controller when tableview header button is clicked 当点击collectionView单元格内的按钮时,如何获取表格视图行并从位于tableViewCell内的collectionView推送到viewController - how to get table view row and push to viewController from collectionView that's inside a tableViewCell when tapped button inside collectionView cell 在Tabelview单元格中单击Collectionview时,如何查找tableview的索引路径 - How to find the indexpath of tableview when Collectionview is clicked which is in tabelview Cell 带有1个数据源的tableview单元格内的Collectionview - Collectionview inside tableview cell with 1 data source 与UITapGestureRecognizer一起使用时,不调用collectionView的didSelectItemAt - collectionView's didSelectItemAt not called when use it with UITapGestureRecognizer tableview单元格内的Collectionview不会增加collectionView的高度 - Collectionview inside tableview cell not increasing height of collectionView collectionview在tableview单元格内重复 - collectionview are repeating inside a tableview cell 如何仅在更新时刷新collectionview或tableview中的数据? - How to refresh the data in collectionview or tableview only for the update? 如何在表格视图中更新集合视图中的数据? - How to update data in a collectionview within a tableview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM