Home > front end >  Why is some of my JSON data not showing in my table view?
Why is some of my JSON data not showing in my table view?

Time:11-12

In one screen I have two table views, so I have written code like this. If I use one table view then I get the whole data, but with two table views I have this issue.

code: Here skillsTableView is not showing the entire JSON data. For testing purposes I have taken skillsArray. If I print this here I get all of the data.

But in my skillsTableView, I don't get the total data. I can't use skillsArray to count because I need every skill's corresponding id too.

Why am I not getting the whole JSON data in my skillsTableView?

private var skillsMasterData = SkillsMasterModel(dictionary: NSDictionary()) {
    didSet {
        skillsArray = skillsMasterData?.result?.skills?.compactMap { $0.skill }
        print("skills array \(skillsArray)")
        skillsTableView.reloadData()
    }
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView == tableView {
        return langCellArray.count
    } else {
        return skillsMasterData?.result?.skills?.count ?? 0
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if tableView == self.tableView {
        let cell = tableView.dequeueReusableCell(withIdentifier: "EditLangTableVIewCell", for: indexPath) as! EditLangTableVIewCell
        let item = langCellArray[indexPath.row]
        cell.langLbl.text = item.name
        return cell
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SkillsTableVIewCell", for: indexPath) as! SkillsTableVIewCell
        
        let item = skillsMasterData?.result?.skills?[indexPath.row]
        cell.skillsLabel.text = item?.skill
        let id = skillsMasterData?.result?.skills?[indexPath.row].id ?? 0
        
        if arrSelectedRows.contains(id) {
            cell.chkImage.image = UIImage(systemName: "checkmark.circle.fill")
        } else {
            cell.chkImage.image = UIImage(systemName: "checkmark.circle")
        }
        return cell
    }
}

CodePudding user response:

Your numberOfRowsInSection method has a mistake when checking the table view.

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView === self.tableView { // <-- Add self. and use ===
        return langCellArray.count
    } else {
        return skillsMasterData?.result?.skills?.count ?? 0
    }
}

The mistake meant that if tableView == tableView was always true and you returned langCellArray.count for both table views.

It's also better to use === than == when comparing object references since in a case like this you want to see if they are the same instances. You are not trying to compare if two object have the same attributes.

  • Related