2017-06-06 79 views
0

我有一个小问题,我的静态tableview。我有一个弹出窗口显示为我的网格选项。我要救我的静态TableView中的状态(indexPath),但似乎它不工作,下面是我的代码片段:Swift:保存并显示选定的indexPath在静态表视图

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    var currSelected: IndexPath? 

    let section = indexPath.section 
    let numberOfRows = tableView.numberOfRows(inSection: section) 
    for row in 0..<numberOfRows { 
     if let cell = tableView.cellForRow(at: NSIndexPath(row: row, section: section) as IndexPath) { 
      cell.accessoryType = row == indexPath.row ? .checkmark : .none 
      tableView.deselectRow(at: indexPath, animated: false) 
      currSelected = indexPath 

      if section == 2 { 
       tableView.deselectRow(at: indexPath, animated: false) 
       cell.accessoryType = .none 
      } 
      else { 
       delegate?.option(lastSelected: currSelected!) 
       NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reload"), object: nil) 

      } 
     } 
    } 
} 

我试图用delegatelastSelected(最后indexPath)以前控制器并将其发送回Pop Over Controller,然后我就得到了它。但我不知道使用lastSelected显示复选标记。而prepare for cell at函数需要一个重用单元的标识符,但我不使用它。

我已阅读this以显示复选标记但未保存状态。我也读过this,但也一样。任何建议/答案将帮助我。在此先谢谢

回答

0
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 
    if indexPath == lastSelected { 
     cell.accessoryType = .Checkmark 
    } 
} 
+0

其唯一的保存一个选中标记。我有2个部分,不能chekced,所以我需要2复选标记,如果lastSelected有两个复选标记 –

+1

然后,您需要存储IndexPaths数组而不是IndexPath变量,并在'willDisplayCell',通过该数组循环 – Malik

+0

其工作与分离索引路径。接受为有用的sugest,我会更新我的最终代码。提前致谢 –

相关问题