簡體   English   中英

如何使用 Swift 在 UITableView 中保存和保留選定的單元格復選標記?

[英]How to save and persist selected cell check mark in UITableView using Swift?

在 myscenario 中,我試圖將JSON數據加載到我的tableview中。 在這里,我在表格視圖中使用多個單元格選擇。 我需要persist選定的單元格復選checkmark 如果我 go 另一個視圖 controller 並返回查看 tableview 選擇的復選標記需要顯示。

我的代碼

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.tableView.deselectRow(at: indexPath, animated: true)
    let item = searching ? filteredData[indexPath.row] : membersData[indexPath.row]

    if selectedRows.contains(indexPath.row) {
        tableView.cellForRow(at: indexPath)?.accessoryType = .none
    } else {
        tableView.cellForRow(at: indexPath)?.accessoryType = 
    }
}

你似乎已經有一個數組什么的

selectedValues

由於您只是在學習,因此只需將其設為全局或 static。

然后在制作每個單元格時檢查它。 真的那么容易

您的代碼如下所示:

class YourScreen: TableView whatever .. {
    var selectedValues: [something]
}

將其更改為全局:

var selectedValues: [something]
class YourScreen: TableView whatever .. {
}

現在在您的tableView#cellForRowAt中,只需根據該全局設置它。 所以像

if selectedValues.contains(item) {.accessoryType = .checkmark}

學習如何“持久化數據”。 在計算機中確實需要大量工作,從頭開始,然后通過一般數據庫 SQL、NoSQL、Firebase 等服務,然后是 Core。 和實時更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM