重新启动ViewController后,Swift Tableview复选标记消失了

就我而言,我正在将JSON数据加载到tableview中。在这里,实现了表格单元格多个单元格选择checkmarkuncheckmark选项。如果转到上一个viewcontroller,然后再次返回tableview控制器,则最后选择的选中标记消失。如何store呢?

JSON可编码

// MARK: - Welcome
struct Root: Codable {
    let status: Bool
    let data: [Datum]
}

// MARK: - Datum
struct Datum: Codable,Hashable {
    let userid,firstname,designation: String?
    let profileimage: String?
}

自定义单元格类

class MyCustomCell: UITableViewCell {
    @IBOutlet weak var profileImage: UIImageView!
    @IBOutlet weak var nameCellLabel: UILabel!
    @IBOutlet weak var subtitleCellLabel: UILabel!
}

Tableview选中标记的代码

 var studentsData = [Datum]()
 var sessionData = Set<Datum>()

    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:MyCustomCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! MyCustomCell
        let item = self.studentsData[indexPath.row]
        cell.nameCellLabel.text = item.firstname
        cell.subtitleCellLabel.text = item.designation
        return cell
    }

    func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath,animated: true)
        let item = self.studentsData[indexPath.row]
        if let cell = tableView.cellForRow(at: indexPath) {
            if cell.accessorytype == .checkmark {
                cell.accessorytype = .none

                // UnCheckmark cell JSON data Remove from array
                self.sessionData.remove(item)
                print(sessionData)

            } else {
                cell.accessorytype = .checkmark

                // Checkmark selected data Insert into array
                self.sessionData.insert(item)
                print(sessionData)
            }
        }
    }
tanyuran 回答:重新启动ViewController后,Swift Tableview复选标记消失了

您应始终将checkMark状态保存在另一个数组或变量中。 如果只能选择一项:

var SELECTED_ITEMS= [YOUR_DATA_TYPE]()//It must be global within tableViewController

在允许多项选择的情况下

   func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
            tableView.deselectRow(at: indexPath,animated: true)
            let item = self.studentsData[indexPath.row]
            if SELECTED_ITEMS.contain(item){
                SELECTED_ITEMS.remove(item)
            }else{
                SELECTED_ITEMS.append(item)
            }
}

请记住,SELECTED_ITEM应该是表视图数据的数组,而SELECTED_ITEM只是表视图数据的同一类型。 另外,如果您要在TableView控制器的ViewDidLoad或ViewWillAppear中初始化模型,则在出现tableview时,请不要重置SELECTED_ITEMSSELECTED_ITEM

然后

func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          ........
           cell.accessoryView = SELECTED_ITEMS.contain(item) ? .checkmark:.none// Multi Selection
          .........

    }

通常,您更新模型,变量或数组或代码中适合的任何内容,以跟踪选择/未选择哪个索引路径。然后在cellForRowAt中,您可以检查上面的变量/数组...以设置附件。

func tableView(_ tableView: UITableView,willDisplay cell: UITableViewCell,forRowAt indexPath: IndexPath){}

也将起作用

,

创建一种将复选标记状态存储在数据结构中的方法

struct Datum: Codable,Hashable {
    let userid,firstname,designation: String?
    let profileimage: String?
    var selected: Bool = false
}

在创建单元格时设置值

func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:MyCustomCell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! MyCustomCell
        let item = self.studentsData[indexPath.row]
        cell.nameCellLabel.text = item.firstname
        cell.subtitleCellLabel.text = item.designation
        cell.accessoryType = item.selected ? .checkmark : .none
        return cell
    }

然后在didSelectRowAt中将if块替换为以下内容,以将更改保存回学生数据,然后相应地重置选中标记:

 self.studentsData[indexPath.row].selected.toggle()
 cell.accessoryType = studentsData[indexPath.row].selected ? .checkmark : .none
本文链接:https://www.f2er.com/3164534.html

大家都在问