繁体   English   中英

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

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

我有一个tableView,在每个单元格中都有一个collectionView。 有时候,当我知道collectionViews中有新数据时,我会在collectionView上调用reloadData() 在大多数情况下,此例程都有效,但有时会开始使用100%的CPU,内存使用量增加,GUI被阻止,并且此时应用程序将崩溃。 如果我不让重新加载collectionView,则一切正常,只有内容在collectionView中无效。 发生了什么,在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

我已经更新了我的代码,例如@dasblinkenlight建议,我不再从cellforRowAtIndexPath调用reloadData了,但是现在当某些用户交互触发它时,将为外面的所有collectionView窗体调用reloadData 但是有时候还是会崩溃,不知道为什么吗?

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

    (sv as! UICollectionView).reloadData()
}

在此处输入图片说明

我没有重新加载嵌套的collectionView ,而是实现了configureCell方法,该方法重置单元格,并为所有可见单元格调用configureCell 那是我的解决办法,因此无限循环可以避免。

暂无
暂无

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

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