2016-11-25 71 views
0

tableview的选定行(.checkmark)值(字符串)保存在sqlite数据库中,并且tableview必须重新载入相同的值,同时显示复选标记。代码段下面给出swift如何从sqlite中重新载入tableview选定行(.checkmark)值

 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

让我们看看你的tableView (tableView:UITableView,numberOfRowsInSection节:Int)方法。 –

+0

func tableView(tableView:UITableView,numberOfRowsInSection section:Int) - > Int { return arr.count } –

+0

那么存储在arr数组中的值是什么? –

回答

0

FUNC的tableView(的tableView没有选中标记:UITableView的, letSelectRowAtIndexPath indexPath:NSIndexPath){let cellN:UITableViewCell = UITableViewCell(); 如果让==器isChecked真正{ cellN.accessoryType = UITableViewCellAccessoryType.Checkmark }其他{ cellN.accessoryType = UITableViewCellAccessoryType.None } 恢复单元 }

您需要检查的cellForRowAtIndexPath委托方法状态显示/隐藏复选标记。

0

您已经添加了cell.accessoryType = .Checkmarkfunc tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)

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

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

但在表格视图重载的情况下,数据源方法func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell被调用时,你有只增加cell.accessoryType = .None'

如果你需要显示选中标记正确地调用下面的代码片段,从我的理解,从读取数据DB,设置accessoryType

let objRegisterModel: RegisterModel = RegisterModelManager.getDBInstance().getRegisterDataByCurrentMedID(uid) 

    if objRegisterModel != "" { 

     medicationFor = objRegisterModel.MedicationFor 
    } 

,然后把前一个条件相应

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

谢谢.. @ DILi,@Harsh Shah&El Tomato我会很快检查并发布结果 –

相关问题