在一個螢屏中,我有兩個表格視圖,所以我撰寫了這樣的代碼。如果我使用一個表視圖,那么我會得到整個資料,但是使用兩個表視圖我會遇到這個問題。
代碼:這里skillsTableView
沒有顯示整個 JSON 資料。出于測驗目的,我采取了skillsArray
. 如果我在這里列印這個,我會得到所有的資料。
但是在我的 中skillsTableView
,我沒有得到總資料。我不能skillsArray
用來計數,因為我也需要每個技能對應的 id。
為什么我的 .json 檔案中沒有完整的 JSON 資料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
}
}
uj5u.com熱心網友回復:
檢查表格視圖時,您的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
了兩個表視圖。
它也===
比==
比較物件參考時更好用,因為在這種情況下,您想查看它們是否是相同的實體。您不是要比較兩個物件是否具有相同的屬性。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/533059.html
上一篇:在多個非托管實體之間共享保留狀態