简体   繁体   中英

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

The second thing, there's no reason to use DispatchQueue.main.async({})

The third thing, though less important, instead of a for loop, you might use something like:

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.

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
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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