jjzjj

ios - UITableViewCell 快速重复每 12 行

coder 2023-09-12 原文

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

最佳答案

由于 Cell 对象被重复使用,您不能依赖它们来存储数据或状态。它们只是对数据模型中数据的看法。您需要在 cellForRowAtIndexPath

中重置选中/未选中状态

记录单元格选择状态的一种技术是使用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)

        }
    }

}

关于ios - UITableViewCell 快速重复每 12 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31820033/

有关ios - UITableViewCell 快速重复每 12 行的更多相关文章

  1. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  2. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  3. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  4. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  5. ruby - 正则表达式 - 保存重复捕获的组 - 2

    这就是我做的a="%span.rockets#diamonds.ribbons.forever"a=a.match(/(^\%\w+)([\.|\#]\w+)+/)putsa.inspect这是我得到的#这就是我想要的#帮助?我尝试过但失败了:( 最佳答案 通常,您不能获得任意数量的捕获组,但如果您使用扫描,您可以为您想要捕获的每个标记获得一个匹配:a="%span.rockets#diamonds.ribbons.forever"a=a.scan(/^%\w+|\G[.|#]\w+/)putsa.inspect["%span","

  6. ruby - 如何以表格格式快速打印 Ruby 哈希值? - 2

    有没有办法快速将表格格式的ruby​​哈希打印到文件中?如:keyAkeyBkeyC...1232343451253474456...其中散列的值是不同大小的数组。还是使用双循环是唯一的方法?谢谢 最佳答案 试试我写的这个gem(在表中打印散列、ruby对象、ActiveRecord对象):http://github.com/arches/table_print 关于ruby-如何以表格格式快速打印Ruby哈希值?,我们在StackOverflow上找到一个类似的问题:

  7. Ruby 从数组中删除重复的对象 - 2

    我无法使用传统的Ruby方法从下面的数组user_list中删除所有重复对象,从而获得预期的结果。有解决这个问题的聪明方法吗?users=[]user_list.eachdo|u|user=User.find_by_id(u.user_id)users 最佳答案 这个怎么样?users=User.find(user_list.map(&:user_id).uniq)这具有作为一个数据库调用而不是user_list.size数据库调用的额外好处。 关于Ruby从数组中删除重复的对象,我们在

  8. Ruby 删除可枚举列表中的重复项 - 2

    ruby中有没有一个很好的方法来删除可枚举列表中的重复项(即拒绝等) 最佳答案 对于数组你可以使用uniq()方法a=["a","a","b","b","c"]a.uniq#=>["a","b","c"]所以如果你只是(1..10).to_a.uniq或%w{antbatcatant}.to_a.uniq因为无论如何,几乎所有您实现的方法都将作为Array类返回。 关于Ruby删除可枚举列表中的重复项,我们在StackOverflow上找到一个类似的问题: h

  9. ruby - 重复排列 - 2

    我知道如何创建值数组的排列。例如:[*1..3].permutation(2)这导致以下六种排列:[1,2][1,3][2,1][2,3][3,1][3,2]但这个结果缺少三个排列,它们是相同值的组合,即:[1,1][2,2][3,3]如何获得所有排列,包括上面重复的排列? 最佳答案 尝试#repeated_permutation:[*1..3].repeated_permutation(3).to_a>pp[*1..3].repeated_permutation(3).to_a[[1,1,1],[1,1,2],[1,1,3],[1

  10. 深度学习12. CNN经典网络 VGG16 - 2

    深度学习12.CNN经典网络VGG16一、简介1.VGG来源2.VGG分类3.不同模型的参数数量4.3x3卷积核的好处5.关于学习率调度6.批归一化二、VGG16层分析1.层划分2.参数展开过程图解3.参数传递示例4.VGG16各层参数数量三、代码分析1.VGG16模型定义2.训练3.测试一、简介1.VGG来源VGG(VisualGeometryGroup)是一个视觉几何组在2014年提出的深度卷积神经网络架构。VGG在2014年ImageNet图像分类竞赛亚军,定位竞赛冠军;VGG网络采用连续的小卷积核(3x3)和池化层构建深度神经网络,网络深度可以达到16层或19层,其中VGG16和VGG

随机推荐