ios - 快速如何从 sqlite 重新加载 tableview 选定的行(.checkmark)值

标签 ios swift sqlite uitableview

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 = .Checkmarkfunc tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

cell.accessoryType = .Nonefunc 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/

相关文章:

android - 无法更新android中sqlite表中的一行

ios - Facebook 最近是否禁止通过 uri 访问公共(public)页面提要?

iOS : Radio Button Programatically Without using any Image

ios - 如何启动短信应用程序

swift 3 : Return from initializer error

android - 在sqlite中连接两个表

ios - UIView 从左到右滑动动画有问题?

ios - UITextView 中的数组项完成

ios - 在 UIWebView 中获取页面标题?

android - 如何从动态 ArrayList 为 Android SQLite 查询编写 WHERE IN 子句