在一个屏幕中我有两个表视图,所以我写了这样的代码。如果我使用一个表视图,那么我会得到整个数据,但如果使用两个表视图,我会遇到这个问题。
**代码:**这里的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
}
}
1条答案
按热度按时间hfwmuf9z1#
您的
numberOfRowsInSection
方法在检查表视图时出错。这个错误意味着
if tableView == tableView
始终为真,并且您为两个表视图返回了langCellArray.count
。当比较对象引用时,使用
===
比==
更好,因为在这种情况下,你想看看它们是否是相同的示例。你不是在试图比较两个对象是否有相同的属性。