TableView 的选定行(.checkmark)值(字符串)保存在sqlite DB中, TableView 必须使用相同的值重新加载,同时也会显示复选标记。下面给出的代码片段
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("selected \(arr[indexPath.row])")
if let cell = medTable.cellForRowAtIndexPath(indexPath) {
if cell.selected {
cell.accessoryType = .Checkmark
}
}
if let selectedrows = tableView.indexPathsForSelectedRows {
print("didDeselectRowAtIndexPath selected rows:\(selectedrows)")
let objRegisterModel: RegisterModel = RegisterModel()
medicationFor = "\(selectedrows)"
objRegisterModel.MedicationFor = medicationFor;
let splitString = objRegisterModel.MedicationFor.componentsSeparatedByString(",")
print("getSelectedrowsValues", splitString)
}
}
func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
print("deselected \(arr[indexPath.row])")
if let cell = medTable.cellForRowAtIndexPath(indexPath) {
cell.accessoryType = .None
}
if let sr = medTable.indexPathsForSelectedRows {
print("didDeselectRowAtIndexPath selected rows:\(sr)")
}
}
重新加载表格 View 时,会显示值,但不会显示复选标记
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = medTable.dequeueReusableCellWithIdentifier(intervalCellIdentifier, forIndexPath: indexPath) as UITableViewCell
cell.accessoryType = .None
cell.textLabel?.text = arr[indexPath.row]
let objRegisterModel: RegisterModel = RegisterModelManager.getDBInstance().getRegisterDataByCurrentMedID(uid)
if objRegisterModel != "" {
medicationFor = objRegisterModel.MedicationFor
}
return cell
}
sqliteDB中保存的值如下所示
**[<NSIndexPath: 0xc000000000200016> {length = 2, path = 0 - 1}]**
最佳答案
您已添加cell.accessoryType = .Checkmark
在func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
和cell.accessoryType = .None
在func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath)
这将确保复选标记在选择时显示,在取消选择时隐藏。
但是在 TableView 重新加载的情况下,数据源方法func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
被称为,您只添加了 cell.accessoryType = .None'
如果您需要正确显示复选标记,请在设置accessoryType之前调用下面的代码片段,根据我的理解,该代码片段从数据库读取数据
let objRegisterModel: RegisterModel = RegisterModelManager.getDBInstance().getRegisterDataByCurrentMedID(uid)
if objRegisterModel != "" {
medicationFor = objRegisterModel.MedicationFor
}
然后相应地设置条件
if (informationShownSelectedinDB) {
cell.accessoryType = .Checkmark
} else {
cell.accessoryType = .None
}
关于ios - 快速如何从 sqlite 重新加载 tableview 选定的行(.checkmark)值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40806270/