如何在滚动tableView时保持选中单元格?

时间:2016-03-17 12:54:12

标签: ios swift uitableview

我想要一个单元格在触摸时保持选中状态,并且再次触摸它以取消选择。这是我的代码无效:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let selectedCell = tableView.cellForRowAtIndexPath(indexPath)
    if let value = selectedCell?.selected.boolValue {
        if !value {
            print("cell was selected and will remain selected") 
            selectedCell?.selected = true
        } else {
            print("cell is deselected")
            selectedCell?.selected = false
        }
    }
}

我该如何实现?

5 个答案:

答案 0 :(得分:1)

记住规则:

  • 将单元格数据存储在模型中
  • UITableViewController仅用于可视化

在您的代码上,按indexPath.row

存储选定的值,例如在数组中

答案 1 :(得分:0)

您可以将cellForRowAtIndex中每个单元格的默认标记值设置为cell.tag = 0

didSelectRowAtIndexPath功能中你可以做这样的事情

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

   let selectedCell = tableView.cellForRowAtIndexPath(indexPath)
   if selectedCell.tag == 0 {
       print("cell was selected")
       selectedCell.tag = 1
   }
   else {
       print("cell was deselected")
       selectedCell.tag = 0
   }

}

答案 2 :(得分:0)

如果您拥有的是一次选定的一行,那么即使没有数据模型(您应该拥有),您也可以拥有ViewController selectedRowIndex的属性,每次选择时都会更新行,并刷新tableView - 作为cellForRowAtIndexPath的一部分,你可以检查indexPath.row == selectedRowIndex并做出你需要的任何视觉变化

定义一个属性来跟踪当前选定的行

var selectedRowIndex = -1

然后在用户选择行

时更新该属性
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    // build up an array of rows to be refreshed
    // the previously selected row (if any)
    // and the current one
    var indexPaths : [NSIndexPath] = []

    // if a row was previous selected, clear the display
    if selectedRowIndex != -1
    {
        indexPaths.append(NSIndexPath(forRow: selectedRowIndex, inSection: 0))
    }

    selectedRowIndex = indexPath.row
    indexPaths.append(NSIndexPath(forRow: selectedRowIndex, inSection: 0))

    // now refresh just those rows
    self.tableView.reloadRowsAtIndexPaths(indexPaths, withRowAnimation: UITableViewRowAnimation.None)

}

然后在重绘行时使用它做一些事情

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as! MyCell

    if indexPath.row == selectedRowIndex
    {
        cell.backgroundColor = UIColor.greenColor()
    }
    else
    {
        cell.backgroundColor = UIColor.whiteColor()
    }
      // everything else
    return cell
}

答案 3 :(得分:0)

好吧,这就是我设法宣布var selectedIndexPath: NSIndexPath?然后在didSelectRowAtindexPath中执行以下操作:

            if selectedIndexPath == indexPath {
                tableView.deselectRowAtIndexPath(indexPath, animated: true)
                selectedIndexPath = nil
            }else{
                tableView.selectRowAtIndexPath(indexPath, animated: true, scrollPosition: .None)
                selectedIndexPath = indexPath
            }

答案 4 :(得分:0)

我的理解是默认处理单元格选择和取消选择 - 您不应该自己弄乱单元格的选定值!

您可能需要根据所选状态更新tableView:cellForRowAtIndexPath:方法中单元格的某些元素。

如果这不是开箱即用和/或您想要自定义此行为,您还应该查看tableView:didDeselectRowAtIndexPath:方法。这两种方法的组合应该允许您完全控制所选状态。