【问题标题】:swift how to reload tableview selected row(.checkmark) values from sqliteswift如何从sqlite重新加载tableview选定的行(.checkmark)值
【发布时间】:2017-04-09 22:19:45
【问题描述】:

tableview 的选定行(.checkmark)值(字符串)保存在 sqlite DB 中,并且必须使用相同的值重新加载 tableview,同时显示复选标记。下面给出的代码sn-p

        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}]**

【问题讨论】:

  • 向我们展示您的 tableView(tableView:UITableView,numberOfRowsInSection section:Int) 方法。
  • func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return arr.count }
  • arr 数组中存储的值是什么?
  • @RajeshM 以下任何解决方案对您有帮助吗?

标签: ios swift sqlite uitableview


【解决方案1】:
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 委托方法中检查条件以显示/隐藏复选标记。

【讨论】:

    【解决方案2】:

    您已在func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) 中添加了cell.accessoryType = .Checkmark

    cell.accessoryType = .Nonefunc tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath)

    这将确保复选标记在选择时显示并在取消选择时隐藏。

    但是在表视图重新加载的情况下,数据源方法func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell被调用,你只添加了cell.accessoryType = .None'

    如果您需要正确显示复选标记,请在设置附件类型之前调用下面的代码 sn-p,据我了解,该代码从 DB 读取数据

    let objRegisterModel: RegisterModel = RegisterModelManager.getDBInstance().getRegisterDataByCurrentMedID(uid)
    
        if objRegisterModel != "" {
    
            medicationFor = objRegisterModel.MedicationFor
        }
    

    然后相应地设置一个条件

    if (informationShownSelectedinDB) {
        cell.accessoryType = .Checkmark
    } else {
        cell.accessoryType = .None
    }
    

    【讨论】:

    • 谢谢..@DILi、@Harsh Shah 和 El Tomato 我会尽快检查并发布结果
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多