所以我有一個 SimpleUITableView會顯示 aUILabel和 a UISwitch。我需要能夠獲取每個開關的值來更新我的物件,然后最終將資料發送到 API。我有一個半作業的概念。但這只有在所有細胞都可見的情況下才有效。之前我只有 5 個專案要檢查。所以我可以簡單地遍歷所有內容并獲取資料。現在我的清單已經增加到 20 多個專案。
我在一定程度上理解為什么當前代碼不起作用。它找到 nil 值。那將是不可見的細胞。
我的大問題是如何捕獲所有單元格的值和所有值的UISwitch值并更新我的物件?
我嘗試使用 KVO 方法。這是我到目前為止所擁有的,我以前從未使用過它并且有點失落:
private var observation: NSKeyValueObservation?
observation = switchCell.observe(\.switchOne, options: [.old, .new]) { value, change in
print("value: \(value.switchOne.isOn)")
print("change old: \(change.oldValue)")
print("change new: \(change.newValue)")
}
我的問題是我不確定我應該為密鑰路徑使用什么。我收到以下警告:
Passing reference to non-'@objc dynamic' property 'switchOne' to KVO method 'observe(_:options:changeHandler:)' may lead to unexpected behavior or runtime trap
然后模擬器不啟動。最簡單的方法/最好的方法是什么?就像我說的,當我完成每個專案時,我的清單上有 2) 個專案,我打開了開關。然后我需要獲取每個專案的值,并用正確的值更新物件。然后我將資料發送到 API 以將資訊存盤到資料庫中。從我正在閱讀的內容來看,這似乎是正確的方向。
uj5u.com熱心網友回復:
將開關的預定義狀態存盤在字典陣列中,例如:
let alreadyYouHaveThis = [["id": 1, "title": "name"...., ], [ "id": 1, "title": "name"....., ],["id": 1, "title": "name"...., ]
just add one more key in the same data like status...
let updatedData = [["status": true, "id": 1, "title": "name"...., ], ["status": true, "id": 1, "title": "name"....., ],["status": false, "id": 1, "title": "name"...., ]
現在該狀態將在 cellforindexpath 方法中用作其他:
func cellforindexpath() ..... {
let status = data[indexPath.item]["status"]
swithc.tag = indexpath.item
setswitchstatus on above value
}
and action for switch :
@Objc func switchStatus(_ switch: UISwitch) {
let status = data[switch.tag]
status != status
data[switch.tag]["status"] = status
....
}
或者可以編輯主資料源并在該資料中添加這些狀態。
如果您不能執行此操作,請告訴我。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/491372.html
