【问题标题】:Performance issue with tableView inside CollectionViewcellUICollectionViewcell 中的 uitableView 的性能问题
【发布时间】:2016-06-20 11:46:56
【问题描述】:

我有一个 ViewController,它有一个集合视图。在每个集合视图单元格内,它们是一个表格视图。由于那个tableView,我面临着一个性能问题。当调用其 cellforItemAtIndexPath 时,我尝试为每个 CollectionViewCell 重新加载数据。因此,我的 CPU 使用率提高了 90%。我的 ViewController 是这样的

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 10
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
}

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

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("MyCustomCollectionViewCell", forIndexPath: indexPath) as! MyCustomCollectionViewCell
        // This line is causing performance issue 
        cell.tableView.reloadData()
        return cell     

}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    return CGSizeMake(40, 40)
}

MyCustomCollectionViewCell 看起来像这样

class ChannelCollectionViewCell: UICollectionViewCell, UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate {


    @IBOutlet weak var tableView: UITableView!
    var delegate: ChannelCollectionViewCellDelegate?


    // MARK: - Table view data source
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 10
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
       return 120
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
                let cell = tableView.dequeueReusableCellWithIdentifier("MyCustomTableViewCell", forIndexPath:
                    indexPath) as! MyCustomTableViewCell
                cell.title = "Testt Title"
                return cell
    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 50
    }
}

能否将 cell.tableView.reloadData() 移到更好的位置,以免导致性能问题

【问题讨论】:

    标签: ios swift uitableview uicollectionview uicollectionviewcell


    【解决方案1】:

    我不这么认为,因为UITableViewUICollectoinView 一样用数据源填充自己,并且当您将另一个UICollectoinViewCell 出列时,您必须调用reloadData()(因为此单元格可能之前已初始化,您必须补充)。

    您可以做的是停止出队(这样做需要您自担风险)。出队是一种内存优化技术,如果collectionView中只有10个单元格,你“可以”直接初始化一个UICollectionViewCell并显示它。但是collectionView仍然会调用cellForRowAtIndexPath所以你必须为每个indexPaths缓存所有的cell,如果你已经初始化了cell,就返回它。但是这种技术会占用大量内存,因此您可能会收到大量内存警告。

    我猜你正在实现类似 Trello 之类的东西(因为 collectionView 中的 tableView 包含大约 120 行的 tableView 对我来说听起来不合逻辑:/)。如果是这种情况,我建议将您的架构更改为 UIPageViewController + UITableViewContoller

    【讨论】:

    • 感谢您的回答。我不能使用 UIPageViewController 因为我必须同时显示三个可以水平滚动的单元格
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-28
    • 1970-01-01
    相关资源
    最近更新 更多