2016-09-14 60 views
0

我有一个UITableView我加载了一些应用程序设置。我需要表格是单选的,并且由于表格保存了设置,因此可能有一些单元格会根据设置启用状态以编程方式选择。indexPathForSelectedRow返回零,但我有一个单元格编程选择

目前,我遇到一个奇怪的行为,其中,如果以编程方式选择一个单元格,然后indexPathForSelectedRow回报nil(当它应该返回我选择的单元格的指数),因此当我在第二电池接头(改变当前选定的设置)我最终选择了两个单元格(即使在我的tableView对象中将allowMultipleSelection设置为false)。

这里是我的代码:

override func viewDidLoad() { 
    tableView.allowsMultipleSelection = false 
} 

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("myCell") 

    if let cell = cell { 
     // tableObject is an array containing settings model 
     let row = tableObject[indexPath.row] 
     cell.textLabel!.text = row.settingValue 
     if row.selected { 
      cell.setSelected(true, animated: false) 
      cell.accessoryType = .Checkmark 
     } 
     cell.tag = row.id 
    } 

    return cell! 
} 

override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? { 
    // oldIndex is always nil for the cell I called setSelected in cellForRowAtIndexPath 
    if let oldIndex = tableView.indexPathForSelectedRow { 
     if let oldCell = tableView.cellForRowAtIndexPath(oldIndex) { 
      tableView.deselectRowAtIndexPath(oldIndex, animated: true) 
      oldCell.accessoryType = .None   
     } 
    } 

    if let cell = tableView.cellForRowAtIndexPath(indexPath) { 
     cell.setSelected(true, animated: true) 
     cell.accessoryType = .Checkmark 
    } 

    return indexPath 
} 

override func tableView(tableView: UITableView, willDeselectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? { 
    if let cell = tableView.cellForRowAtIndexPath(indexPath) { 
     cell.accessoryType = .None 
     tableView.deselectRowAtIndexPath(indexPath, animated: true) 
    } 
    return indexPath 
} 

任何想法,我怎么能总是在同一时间只选择了一个单元格?

谢谢!

+1

你需要使用DIDSELECT不将选择和方法,你已经有了indexpath你为什么不喜欢tableView.indexPathForSelectedRow –

+0

@NitinGohel代码带上面的代码甚至当我改变它使用didSelectRow我仍然观察到相同的行为 – mradzinski

+0

让我发表一个答案给我几个薄荷 –

回答

0

万一别人遇到此相同的行为,似乎穿越cell.setSelected()选择小区是不一样的调用tableView.selectRowAtIndexPath()方法。选择最新的行可以完美地完成这项工作并解决问题。

0

创建像

var arrSelectCell = [NSIndexPath]() 

数组并做代码在didSelectRowAtIndexPath类似以下内容:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     if arrSelectCell.contains(indexPath) { 
     if let oldCell = tableView.cellForRowAtIndexPath(indexPath) { 

       if let index = arrSelectCell.indexOf(indexPath) { 
        arrSelectCell.removeAtIndex(index) 
       } 
      tableView.deselectRowAtIndexPath(indexPath, animated: true) 
      oldCell.accessoryType = .None 
     } 

    } 
    else 
    { 
     arrSelectCell.append(indexPath) 
     if let selectCell = tableView.cellForRowAtIndexPath(indexPath) { 
      tableView.deselectRowAtIndexPath(indexPath, animated: true) 
      selectCell.accessoryType = .Checkmark 
     } 


    }  

,也不要忘记在cellForRowAtIndexPath设置一个代码来检查已检查后未选中单元格保持重用细胞也。因此需要编写如下几个代码。

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

      let cell = tableView.dequeueReusableCellWithIdentifier("serchCell", forIndexPath: indexPath) as! SearchTableViewCell 

      if arrSelectCell.contains(indexPath) 
      { 
        cell.accessoryType = .Checkmark 
      } 
      else 
      { 
        cell.accessoryType = .None 
      } 

      return cell 
     } 
+0

此代码不起作用,因为'ArrSelectedCell'从未被初始化。为什么它是可选的,并以大写字母开头? – vadian

+0

对资本没有任何限制,这不是一个客观的C,所以不需要初始化。让我告诉你截图以及 –

+0

这不知何故,感觉像一个黑客给我。 '''indexPathForSelectedRow'''应该返回一个'''NSIndexPath'''对象,无论我使用'''setSelected'''设置我的单元格为选中状态,还是用户点击并选中它。 – mradzinski

0

请注意,致电tableView.reloadData()会将tableView.indexPathForSelectedRow重置为零。

这里如何通过tableView.indexPathForSelectedRow完成表格视图单项选择:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
    { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) 
     cell.accessoryType = tableView.indexPathForSelectedRow == indexPath ? .checkmark : .none 
     return cell 
    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
    { 
     if let visiblePaths = tableView.indexPathsForVisibleRows 
     { 
      for visibleIndexPath in visiblePaths 
      { 
       let cell = tableView.cellForRow(at: visibleIndexPath) 
       if visibleIndexPath == indexPath 
       { 
        cell?.accessoryType = .checkmark 
       } 
       else 
       { 
        cell?.accessoryType = .none 
       } 
      } 
     } 
    } 
相关问题