草庐IT

ios - UICollectionView reloadSection 不会从 View 层次结构中删除旧 View

coder 2024-01-13 原文

UICollectionView 在重新加载部分时不会从 View 层次结构中删除可重用 View 。这会为 UIAutomation 带来问题。

我创建了一个简单的应用程序,它使用 UICollectionView 并显示页眉、页脚和单元格。点击“Reload”时,collectionView 显示不同数量的单元格。请引用下图。

启动应用程序时,它显示 2 个单元格。第一次点击“Reload”显示 8 个单元格,第二次点击“Reload”显示 18 个单元格。第三次点击“重新加载”显示 2 个单元格。现在,此时如果我们使用 Xcode View debugger 调试 View ,当禁用“Show only displayed Views”时,它会在 View 层次结构中显示多个单元格。引用下图:

这会在 UIAutomation 获取元素时产生问题。我不确定我的实现有什么问题。

下面是我的 Controller 代码:

struct CellData {
    var title: String
    var value: String
}

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    @IBOutlet var collectionView: UICollectionView!
    private var data: [CellData]!
    private var data1: [CellData]!
    private var data2: [CellData]!
    private var data3: [CellData]!
    private var reloadIndex = 1
    private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.data1 = [
            CellData(title: "Cell Title 1", value: "Value 1"),
            CellData(title: "Cell Title 2", value: "Value 2")]
        self.data = self.data1

        self.data2 = [
            CellData(title: "Cell Title 1", value: "Value 1"),
            CellData(title: "Cell Title 2", value: "Value 2"),
            CellData(title: "Cell Title 3", value: "Value 3"),
            CellData(title: "Cell Title 4", value: "Value 4"),
            CellData(title: "Cell Title 5", value: "Value 5"),
            CellData(title: "Cell Title 6", value: "Value 6"),
            CellData(title: "Cell Title 7", value: "Value 7"),
            CellData(title: "Cell Title 8", value: "Value 8")]

        self.data3 = [
            CellData(title: "Cell Title 1", value: "Value 1"),
            CellData(title: "Cell Title 2", value: "Value 2"),
            CellData(title: "Cell Title 3", value: "Value 3"),
            CellData(title: "Cell Title 4", value: "Value 4"),
            CellData(title: "Cell Title 5", value: "Value 5"),
            CellData(title: "Cell Title 6", value: "Value 6"),
            CellData(title: "Cell Title 7", value: "Value 7"),
            CellData(title: "Cell Title 8", value: "Value 8"),
            CellData(title: "Cell Title 9", value: "Value 9"),
            CellData(title: "Cell Title 10", value: "Value 10"),
            CellData(title: "Cell Title 11", value: "Value 11"),
            CellData(title: "Cell Title 12", value: "Value 12"),
            CellData(title: "Cell Title 13", value: "Value 13"),
            CellData(title: "Cell Title 14", value: "Value 14"),
            CellData(title: "Cell Title 15", value: "Value 15"),
            CellData(title: "Cell Title 16", value: "Value 16"),
            CellData(title: "Cell Title 17", value: "Value 17"),
            CellData(title: "Cell Title 18", value: "Value 18")]

        self.collectionView.registerNib(UINib(nibName: "CollectionCell", bundle: nil), forCellWithReuseIdentifier: "CollectionCell")

        self.collectionView.registerNib(UINib(nibName: "SectionHeader", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "SectionHeader")

        self.collectionView.registerNib(UINib(nibName: "SectionFooter", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionFooter, withReuseIdentifier: "SectionFooter")
    }

    @IBAction func reload() {
        self.activityIndicator.center = self.view.center
        self.view.addSubview(self.activityIndicator)
        self.activityIndicator.startAnimating()
        self.view.userInteractionEnabled = false

        let waitTime = 2 * Double(NSEC_PER_SEC)
        let dispatchTime = dispatch_time(dispatch_time_t(DISPATCH_TIME_NOW), Int64(waitTime))
        dispatch_after(dispatchTime, dispatch_get_main_queue()) {
            self.reloadIndex += 1

            if self.reloadIndex > 3 {
                self.reloadIndex = 1
            }
            switch self.reloadIndex {
            case 2:
                self.data = self.data2
                break
            case 3:
                self.data = self.data3
                break
            default:
                self.data = self.data1
            }
            self.collectionView.reloadSections(NSIndexSet(index: 0))
            //self.collectionView.reloadData()

            self.activityIndicator.stopAnimating()
            self.activityIndicator.removeFromSuperview()
            self.view.userInteractionEnabled = true
        }
    }

    @IBAction func print() {
        printViewHierarchyInJSON(self.view)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.data.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

        let compatibleCell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionCell", forIndexPath: indexPath)
        (compatibleCell as? CollectionCell)?.setData(self.data[indexPath.item])
        return compatibleCell
    }

    func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

        var supplementaryView = UICollectionReusableView()

        switch kind {
        case UICollectionElementKindSectionHeader:
            supplementaryView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader, withReuseIdentifier: "SectionHeader", forIndexPath: indexPath)

        case UICollectionElementKindSectionFooter:
            supplementaryView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionFooter, withReuseIdentifier: "SectionFooter", forIndexPath: indexPath)

        default:
            break
        }

        return supplementaryView
    }


    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
        return CGSizeMake(collectionView.bounds.size.width, 50)
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSizeMake(collectionView.bounds.size.width, 104)
    }

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        return CGSizeMake(collectionView.bounds.size.width, 86)
    }
}

我已经在 dropbox 上上传了这个示例应用程序.

提前谢谢你。

最佳答案

因为每次重新加载一组不同的元素时:

self.reloadIndex += 1

if self.reloadIndex > 3 {
    self.reloadIndex = 1
}

switch self.reloadIndex {
case 2:
    self.data = self.data2        
case 3:
    self.data = self.data3        
default:
    self.data = self.data1
}

关于ios - UICollectionView reloadSection 不会从 View 层次结构中删除旧 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41426797/

有关ios - UICollectionView reloadSection 不会从 View 层次结构中删除旧 View的更多相关文章

  1. ruby - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式 - 2

    我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h

  2. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  3. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  4. ruby - Highline 询问方法不会使用同一行 - 2

    设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案

  5. ruby-on-rails - 项目升级后 Pow 不会更改 ruby​​ 版本 - 2

    我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby​​版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby​​版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘

  6. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  7. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  8. 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返回它复制的字节数,但是当我还没有下

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

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

  10. ruby - 是否有用于序列化和反序列化各种格式的对象层次结构的模式? - 2

    给定一个复杂的对象层次结构,幸运的是它不包含循环引用,我如何实现支持各种格式的序列化?我不是来讨论实际实现的。相反,我正在寻找可能会派上用场的设计模式提示。更准确地说:我正在使用Ruby,我想解析XML和JSON数据以构建复杂的对象层次结构。此外,应该可以将该层次结构序列化为JSON、XML和可能的HTML。我可以为此使用Builder模式吗?在任何提到的情况下,我都有某种结构化数据-无论是在内存中还是文本中-我想用它来构建其他东西。我认为将序列化逻辑与实际业务逻辑分开会很好,这样我以后就可以轻松支持多种XML格式。 最佳答案 我最

随机推荐