2017-07-28 57 views
1

試圖開發一個檢查單應用程序,並且一直試圖保存選中標記的狀態。當我離開tableView並返回所有保存的複選標記時,它們將被刪除。我已經導入UKIT,然後定義了這個類。如何將單選標記附件保存在單元格中

這裏是我的代碼:

var PreDefinedTasks = ["1", "2", "3", "4"] 

// MARK: - Table view data source 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    tableView.deselectRow(at: indexPath, animated: true) 

    if let cell = tableView.cellForRow(at: indexPath as IndexPath) { 
     if cell.accessoryType == .checkmark{ 
      cell.accessoryType = .none 
     } 
     else{ 
      cell.accessoryType = .checkmark 
     } 
    } 
} 

override func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return PreDefinedTasks.count 
} 


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "List1", for: indexPath) 

    cell.textLabel?.text = PreDefinedTasks[indexPath.row] 

    return cell 
} 

我已經看着NSCoder作爲一個解決方案,但不能似乎得到它才能正常工作。任何幫助表示讚賞!

+0

您可以爲選中和未選中的項目保留一個列表數組,並根據它設置cell.assessoryType。 –

回答

1

下面是我如何去做,如果你遵循整個解決方案,即使應用程序關閉,它也會保存。

使一個這樣的數組斷型布爾:var checkmarks = [Int : Bool]()

然後,在cellForRow功能,補充一點:

if checkmarks[indexPath.row] != nil { 
    cell.accessoryType = checkmarks[indexPath.row] ? .checkmark : .none 
} else { 
    checkmarks[indexPath.row] = false 
    cell.accessoryType = .none 
} 

而在didSelectRow功能,補充一點:

if let cell = tableView.cellForRow(at: indexPath as IndexPath) { 
    if cell.accessoryType == .checkmark{ 
     cell.accessoryType = .none 
     checkmarks[indexPath.row] = false 
    } 
    else{ 
     cell.accessoryType = .checkmark 
     checkmarks[indexPath.row] = true 
    } 
} 

如果您希望在應用程序關閉時保存它,則必須通過以下操作將檢查標記數組保存在UserDefaults中:

在didSelectRow功能,在一切後,底部補充一點:

UserDefaults.standard.set(NSKeyedArchiver.archivedData(withRootObject: checkmarks), forKey: "checkmarks") 
UserDefaults.standard.synchronize() 

然後又在viewDidLoad中,補充一點:

if let checks = UserDefaults.standard.value(forKey: "checkmarks") as? NSData { 
    checkmarks = NSKeyedUnarchiver.unarchiveObject(with: checks as Data) as! [Int : Bool] 
} 

讓我知道如果這個答案幫助,如果您有任何疑問。

編輯: 所以我完全忘了,[Int:Bool]不是一個NSDictionary,它只是一個字典。 UserDefaults不能存儲Dictionary對象,只能使用NSDictionary,這就是爲什麼你必須將它更改爲NSData,因此它能夠保存[Int:Bool]。希望它這次有效:)

+0

謝謝,這的確幫助我更深入地理解數據持久性。即使應用程序關閉,我如何更改此數據以保留數據?再次,謝謝你的回覆:)編輯:我無法看到你的答案的其餘部分抱歉。我討厭iMacs :( –

+0

我很奇怪的措辭,但我的答案的最後一部分顯示了當應用程序關閉時如何保存 – Timmy

+0

是啊對不起,你的措辭很好,非常簡潔,謝謝你的幫助 –

0

您可以根據tableView行上選中的複選標記將索引路徑保存到數組中。

+0

你能建議我會怎麼做嗎?感謝您的回覆:) –

+0

因此,在您的didSelectRow委託方法中,您可以獲得已選擇哪些行的索引路徑。你可以存儲在數組中,當你重新加載你的表時,如果匹配,檢查indexpath設置單元格上的複選標記。 –

0

根據你的實現,UITableViewCell被重用與標識符爲「List1」。所以如果你想重新使用這個單元格,那麼你必須通過將狀態存儲在預定義的任務中來更新正確的accessoryType。

0

由於單元格已卸載並稍後重用,因此您需要在其他位置存儲複選標記的狀態。在這個例子中名爲preDefinedTaskCheckmarkState的數組中。加載單元格時,您還需要設置複選標記狀態。

var PreDefinedTasks = ["1", "2", "3", "4"] 
var preDefinedTaskCheckmarkState = [Bool](repeating: false, count: 4) 

// MARK: - Table view data source 

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    tableView.deselectRow(at: indexPath, animated: true) 

    if let cell = tableView.cellForRow(at: indexPath as IndexPath) { 

     preDefinedTaskCheckmarkState[indexPath.row] = !(cell.accessoryType == .checkmark) 

     cell.accessoryType = preDefinedTaskCheckmarkState[indexPath.row] ? .checkmark : .none 
    } 
} 

override func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return PreDefinedTasks.count 
} 


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "List1", for: indexPath) 

    cell.textLabel?.text = PreDefinedTasks[indexPath.row] 
    cell.accessoryType = preDefinedTaskCheckmarkState[indexPath.row] ? .checkmark : .none 

    return cell 
} 
+0

這是一種我沒有考慮的方法。謝謝你的回覆是有用的:) –

+0

沒問題,希望它能幫你一下:) – Christoph

相關問題