【问题标题】:Tableview scroll changes selection表格视图滚动更改选择
【发布时间】:2017-08-20 16:28:20
【问题描述】:

我查看了几个与此相关的 StackOverflow 问题,但似乎还没有很好地理解这个概念。

我有一个表格视图,其中包含我使用枚举填充的几个单元格。当我向下滚动行时,表格中的另一行被选中。我实现了以下方法:

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

    var cell = tableView.dequeueReusableCellWithIdentifier("activityCell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = activities[indexPath.item].rawValue;

    return cell;
}

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

    var cell = tableView.cellForRowAtIndexPath(indexPath);
    cell?.selected = true;

}

我知道细胞是可重复使用的,但我无法正确理解这个概念以及它是如何应用的。

你能给我一些帮助吗?

干杯

【问题讨论】:

  • 那么问题是在您不希望在表格中向上滚动时选择多个单元格吗?
  • 一种 'selectedCells' 数组保存选定单元格的索引... 然后调用 cellForRowAtIndexPath 方法,它可以检查该索引是否在数组中,从而将单元格设置为选定。 . 想到的第一个想法:)

标签: ios swift


【解决方案1】:

如果您要重用单元格,按照您所做的方式在 indexPath 处的 didSelectRowAtIndexPath 中将单元格设置为 selected 将使重用单元格也反映更改后的 selected 属性。相反,我建议向您的活动对象添加一个布尔型 selected 属性,以便可以在 cellForRowAtIndexPath: 中进行选择更改,例如:

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

    var cell = tableView.dequeueReusableCellWithIdentifier("activityCell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = activities[indexPath.item].rawValue
    cell?.selected = activities[indexPath.item].selected
    return cell;
}

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

    activities[indexPath.item].selected = true
    self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)

}

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

    activities[indexPath.item].selected = false
    self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.None)

}

【讨论】:

  • 感谢 Lyndsey 的帮助。
  • @FilipeLouro 很高兴听到:)
【解决方案2】:

正如 Byron 所建议的,我们可以通过保存选定的 数组中的索引路径,并在未选中时将它们从数组中删除。

var indexArray  : [NSIndexPath]?

在加载表之前初始化indexArray

self.indexArray = []

单元格的选择和取消选择必须在 indexArray 的帮助下反映在委托函数中:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
                cell.accessoryType = .checkmark
                self.indexArray?.append(indexPath as NSIndexPath)
        }
       }

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {

        if let cell = tableView.cellForRow(at: indexPath as IndexPath) {
            cell.accessoryType = .none
            if let pos = self.indexArray?.index(of: indexPath as NSIndexPath)
            {
                self.indexArray?.remove(at: pos)
            }
        }
       }

现在,当为每一行调用 cellForRowAtIndexpath 函数时,检查当前 indexPath 是否存在于我们的 indexArray 中。如果是,则该单元格已被选中,否则,该单元格处于未选中状态并在该单元格上执行相应的任务。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "yourCellIdentifier") as! YourTableViewCell

    if (self.indexArray?.contains(indexPath as NSIndexPath))!
    {
        cell.accessoryType = .checkmark
    }
    else
    {
        cell.accessoryType  = .none
    }
    return cell
}

【讨论】:

    猜你喜欢
    • 2013-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 2011-04-08
    • 1970-01-01
    • 2016-06-03
    • 1970-01-01
    相关资源
    最近更新 更多