ios - 重新启动 View Controller 后,Swift Tableview 复选标记消失

标签 ios swift uitableview

在我的例子中,我正在将 JSON 数据加载到 tableview 中。在这里,实现了 tableview 单元格多单元格选择 checkmarkuncheckmark 选项。如果转到上一个 viewcontroller 并再次返回 tableview controller,则最后选择的复选标记消失。如何存储它?

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 Checkmark代码

 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)
            }
        }
    }

最佳答案

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

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 block 替换为以下内容以将更改保存回学生数据,然后相应地重置复选标记:

 self.studentsData[indexPath.row].selected.toggle()
 cell.accessoryType = studentsData[indexPath.row].selected ? .checkmark : .none

关于ios - 重新启动 View Controller 后,Swift Tableview 复选标记消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58699088/

相关文章:

iphone - 使用 Image Magick 对 iPhone 中的图像进行边缘检测

ios - 我想为每个 VC 设置导航颜色为不同颜色

ios - 如何在 iOS 中向超过 2000 个收件人发送邮件?

iphone - UITableViewCell 重用和图像重新渲染

ios - 允许在 uitableview 中编辑,但不能在一行中编辑

iphone - 当我在 UITextField 外部单击到 UITableView 上时,如何退出FirstResponder

iOS 委托(delegate)方法未调用

ios - 如何从 Parse.com 获取单个键的所有值

ios - 检查 NSArray 是否包含具有特定属性的对象

swift - 在 Swift 3 中解包可选整数的正确方法