就我而言,我正在将JSON
数据加载到tableview
中。在这里,实现了表格单元格多个单元格选择checkmark
和uncheckmark
选项。如果转到上一个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)
}
}
}