简体   繁体   English

使用UICollectionView在iOS 9中奇怪的100%CPU使用率

[英]Strange 100% CPU usage in iOS 9 with UICollectionView

I have a tableView, in each cell there is a collectionView. 我有一个tableView,在每个单元格中都有一个collectionView。 Sometimes when I know there is new data in collectionViews, I call reloadData() on collectionView. 有时候,当我知道collectionViews中有新数据时,我会在collectionView上调用reloadData() Most of the time this routine works, but but sometimes is starts to use 100% of CPU, memory usage increasing, GUI is blocked, and at a point app will crash. 在大多数情况下,此例程都有效,但有时会开始使用100%的CPU,内存使用量增加,GUI被阻止,并且此时应用程序将崩溃。 If I do not let reload collectionView, then all works well, only the content invalid in the collectionView. 如果我不让重新加载collectionView,则一切正常,只有内容在collectionView中无效。 What is going on, in iOS 8 it was not a problem to call reloadData() from cellForRowAtIndexPath . 发生了什么,在iOS 8中,从cellForRowAtIndexPath调用reloadData()并不是问题。

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

    let cell = tableView.dequeueReusableCellWithIdentifier("movie") as! MovieTableViewCell

    //..

    if cell.rcvc == nil {

        let cvfl = UICollectionViewFlowLayout()
        cvfl.scrollDirection = .Horizontal
        cell.rcvc = RatingCollectionViewController(collectionViewLayout: cvfl)

        self.addChildViewController(cell.rcvc!)
        let v = cell.viewWithTag(5)!
        v.addSubview(cell.rcvc!.view)
        cell.rcvc!.didMoveToParentViewController(self)
        cell.rcvc!.view.frame = v.bounds
    }

    cell.rcvc!.movie = movie

    if enableReloadRatingCollectionViews {

        cell.rcvc!.collectionView!.reloadData() // <--- if I comment, not crashes
    }

    return cell
}

UPDATE UPDATE

I have updated my code like @dasblinkenlight suggest, I am not calling reloadData anymore from cellforRowAtIndexPath , but now call reloadData for all collectionView s form outside when certain user interaction triggers it. 我已经更新了我的代码,例如@dasblinkenlight建议,我不再从cellforRowAtIndexPath调用reloadData了,但是现在当某些用户交互触发它时,将为外面的所有collectionView窗体调用reloadData But it still crashes sometimes, any idea why? 但是有时候还是会崩溃,不知道为什么吗?

itemTableViewController!.tableView.reloadData()
for sv in collectionViews.allObjects {

    (sv as! UICollectionView).reloadData()
}

在此处输入图片说明

Instead of reloading the nested collectionView , I implemented a configureCell method, that reset cell, and for all the visible cell I call configureCell . 我没有重新加载嵌套的collectionView ,而是实现了configureCell方法,该方法重置单元格,并为所有可见单元格调用configureCell That is my fix, and so the infinite loop can avoid. 那是我的解决办法,因此无限循环可以避免。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM