【问题标题】:UITableViewCell repeat every 12 rows in swiftUITableViewCell 快速每 12 行重复一次
【发布时间】:2015-10-27 11:49:36
【问题描述】:

使用以下代码,当我单击一个单元格以创建复选标记附件时,它每 12 行重复一次复选标记。关于为什么的任何想法?

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

          let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as? UITableViewCell

          cell?.textLabel = "\(indexPath.row)"

          return cell!

      }


      func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {


         if let cell = tableView.cellForRowAtIndexPath(indexPath) as? UITableViewCell {

              if cell.accessoryType == UITableViewCellAccessoryType.Checkmark
              {
                 cell.accessoryType = UITableViewCellAccessoryType.None
              }
              else
              {
                  cell.accessoryType = UITableViewCellAccessoryType.Checkmark
               }
          }

         return indexPath
      }

【问题讨论】:

  • 因为你正在使用 dequeueReusableCellWithIdentifier
  • Nakib 因为它是一个自定义的 UITableViewCell
  • @Nikab 没有问“你为什么使用 dequeueReusableCellWithIdentifier?”他们只是告诉你为什么会出现问题。

标签: ios swift uitableview


【解决方案1】:

由于 Cell 对象被重复使用,您不能依赖它们来存储数据或状态。它们只是您在数据模型中拥有的数据的视图。你需要在cellForRowAtIndexPath中重置checked/non-checked状态

记录单元格选择状态的一种技术是使用Set 来存储选择的indexPaths。这是一个展示这种技术的简单示例 -

class ViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {

    var checkedRows=Set<NSIndexPath>()

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 100     // Simple example - 100 fixed rows
    }

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

        cell.textLabel!.text="Row \(indexPath.row)"    

        cell.accessoryType=self.accessoryForIndexPath(indexPath)


        return cell
    }

    func accessoryForIndexPath(indexPath: NSIndexPath) -> UITableViewCellAccessoryType {

        var accessory = UITableViewCellAccessoryType.None

        if self.checkedRows.contains(indexPath) {
            accessory=UITableViewCellAccessoryType.Checkmark
        }

        return accessory
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        tableView.deselectRowAtIndexPath(indexPath, animated: true)

        if checkedRows.contains(indexPath) {
            self.checkedRows.remove(indexPath)
        } else {
            self.checkedRows.insert(indexPath)
        }

        if let cell=tableView.cellForRowAtIndexPath(indexPath) {
            cell.accessoryType=self.accessoryForIndexPath(indexPath)

        }
    }

}

【讨论】:

  • 不如在 UITableView 上使用 indexPathsForSelectedRows 属性而不是重新发明它?
  • 如果您使用该属性,那么您必须保持选中的选中单元格,在我看来,这在视觉上不是很吸引人。此外,您必须不断迭代 selected rows 属性,因此效率不是特别高
  • cell.selectionStyle = UITableViewCellSelectionStyleNone 解决了视觉方面的问题。并且 O(1) 访问与 O(n) 访问并不是真正的问题,除非你有成千上万个单元格。我只是说 UITableView API 足以解决原始问题,而无需引入任何额外的逻辑。
  • 随意使用该技术添加您自己的答案。就个人而言,我喜欢瞬时选择突出显示的视觉反馈。这种技术还有助于简单地持久化/取消持久化检查状态 - 您可以使用集合中的数据对象而不是 NSIndexPath
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-14
  • 1970-01-01
  • 1970-01-01
  • 2012-03-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多