Ios UITableViewCell在swift中每12行重复一次

Ios UITableViewCell在swift中每12行重复一次,ios,swift,uitableview,Ios,Swift,Uitableview,使用以下代码,当我单击一个单元格以创建复选标记附件时,它会每隔12行重复一次复选标记。你知道为什么吗 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexP

使用以下代码,当我单击一个单元格以创建复选标记附件时,它会每隔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
      }

由于单元对象是重用的,所以不能依赖它们来存储数据或状态。它们只是数据模型中数据的视图。您需要在
cellforrowatinexpath

记录单元选择状态的一种技术是使用
Set
存储所选的
indexpath
。下面是一个简单的例子,展示了这种技术-

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)

        }
    }

}
类ViewController:UIViewController、UITableViewDataSource、UITableViewDelegate{
var checkedRows=Set()
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回100//简单示例-100个固定行
}
func tableView(tableView:UITableView,cellForRowAtIndexPath:nsindepath)->UITableView单元格{
var cell=tableView.dequeueReusableCellWithIdentifier(“cell”,forIndexPath:indexPath)作为!UITableViewCell
cell.textLabel!.text=“行\(indexPath.Row)”
cell.accessoryType=self.accessoryForIndexPath(indexPath)
返回单元
}
func accessoryForIndexPath(indexPath:NSIndexPath)->UITableViewCellAccessoryType{
var附件=UITableViewCellAccessoryType.None
如果self.checkedRows.contains(indexPath){
附件=UITableViewCellAccessoryType.Checkmark
}
返回附件
}
func tableView(tableView:UITableView,didSelectRowAtIndexPath:nsindepath){
tableView.declerowatinexpath(indexPath,动画:true)
如果checkedRows.contains(indexPath){
self.checkedRows.remove(indexPath)
}否则{
self.checkedRows.insert(indexPath)
}
如果let cell=tableView.cellforrowatinexpath(indexPath){
cell.accessoryType=self.accessoryForIndexPath(indexPath)
}
}
}

因为您正在使用dequeueReusableCellWithIdentifierNakib,因为它是一个自定义的UITableViewCell@Nikab没有问“为什么要使用dequeueReusableCellWithIdentifier?”他们刚刚告诉您问题发生的原因。在UITableView上使用indexPathsForSelectedRows属性而不是重新创建它如何?如果使用该属性,则必须选择选中的单元格,在我看来,这在视觉上不是很吸引人。此外,您必须不断迭代selected rows属性,因此它不是特别有效的cell.selectionStyle=UITableViewCellSelectionStyleNone解决了可视方面的问题。而O(1)访问与O(n)访问并不是一个真正的问题,除非你有数千个单元。我想说的是,UITableView API足以解决原始问题,而无需引入任何额外的逻辑。请随意使用该技术添加您自己的答案。就我个人而言,我喜欢瞬间选择突出显示的视觉反馈。这种技术还可以简单地持久化/取消持久化已检查状态—您可以使用集合中的数据对象,而不是NSIndexPath