为什么我的一些JSON数据没有显示在我的表视图中?

svmlkihl  于 2022-11-19  发布在  其他
关注(0)|答案(1)|浏览(184)

在一个屏幕中我有两个表视图,所以我写了这样的代码。如果我使用一个表视图,那么我会得到整个数据,但如果使用两个表视图,我会遇到这个问题。

**代码:**这里的skillsTableView没有显示全部JSON数据。出于测试的目的,我使用了skillsArray。如果我在这里打印它,我会得到所有的数据。

但是在我的skillsTableView中,我没有得到总的数据。我不能用skillsArray来计数,因为我还需要每个技能对应的id。
为什么我的skillsTableView中没有得到完整的JSON数据?

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
    }
}
hfwmuf9z

hfwmuf9z1#

您的numberOfRowsInSection方法在检查表视图时出错。

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

这个错误意味着if tableView == tableView始终为真,并且您为两个表视图返回了langCellArray.count
当比较对象引用时,使用=====更好,因为在这种情况下,你想看看它们是否是相同的示例。你不是在试图比较两个对象是否有相同的属性。

相关问题