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)")
}
}
在重新加载tableview时,会显示值,但不会显示复选标记
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}]**
答案 0 :(得分:0)
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cellN:UITableViewCell = UITableViewCell();
if let isChecked == true{
cellN.accessoryType = UITableViewCellAccessoryType.Checkmark
}else{
cellN.accessoryType = UITableViewCellAccessoryType.None
}
return cell
}
您需要检查cellForRowAtIndexPath委托方法中的条件以显示/隐藏复选标记。
答案 1 :(得分:0)
您已在cell.accessoryType = .Checkmark
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
在cell.accessoryType = .None
中和func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath)
这将确保选中时显示复选标记,并在取消选择时隐藏。
但是如果表视图重新加载,则会调用数据源方法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
}