Home > Net >  Why all JSON data not showing in tableview in swift
Why all JSON data not showing in tableview in swift

Time:11-12

In one screen i have two tableviews, so i have written code like this.. if i use one tableview then i am getting whole data but with two tableviews facing issue

code: here skillsTableView not showing whole json data, for testing purpose i have taken skillsArray if i print this here i am getting whole data

but in my skillsTableView i am not getting total data.. i cant use skillsArray to count because i need every skills corresponding id also

why i am not getting total json data in my skillsTableView where am i wrong, please guide me.

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