繁体   English   中英

具有 collectionView 性能问题的 TableView 单元格

[英]TableView cells with collectionView performance issue

我有以下布局: 在此处输入图片说明 当滚动 tableView fps 下降到 25 左右时,它变得非常抖动。

我认为这是我对 collectionView 的实现,因为在 cellForIndexAtPath 中执行了很多操作。 但是我看不到可以优化什么?

extension MyTableViewController: UICollectionViewDelegate, UICollectionViewDataSource {

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
    // No sections are longer than 99
    if(collectionView.tag < 100)
    {
        let code = codes0[collectionView.tag].objectForKey("code") as! String

        return code.componentsSeparatedByString(",").count
    }
    else if(collectionView.tag < 200)
    {
        let code = codes1[collectionView.tag-100].objectForKey("code") as! String

        return code.componentsSeparatedByString(",").count
    }
    else
    {
        let code = codes2[collectionView.tag-200].objectForKey("code") as! String

        return code.componentsSeparatedByString(",").count
    }
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
{
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("code", forIndexPath: indexPath) as! ImageCodeCollectionViewCell

    // Read that this should help for GPU
    cell.layer.shouldRasterize = true
    cell.layer.rasterizationScale = UIScreen.mainScreen().scale

    var codeLength:String! = ""

    if(collectionView.tag < 100)
    {
        codeLength = self.codes0[collectionView.tag].objectForKey("code") as! String
    }
    else if(collectionView.tag < 200)
    {
        codeLength = self.codes1[collectionView.tag-100].objectForKey("code") as! String
    }
    else
    {
        codeLength = self.codes2[collectionView.tag-200].objectForKey("code") as! String
    }

    let cLength = codeLength.componentsSeparatedByString(",")

    if(indexPath.row <= cLength.count)
    {
        cell.imageCode.image = UIImage(named: cLength[indexPath.row])
    }

    return cell
}

我的 tableView 看起来像:

这是 collectionView 数据源和委托集

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier("cheat") as! CodeTableViewCell

    cell.layer.shouldRasterize = true
    cell.layer.rasterizationScale = UIScreen.mainScreen().scale

    cell.setCollectionViewDataSourceDelegate(self, forRow: indexPath.row, forSection: indexPath.section)

    return cell
}

func tableView(tableView: UITableView, willDisplayCell cell: CodeTableViewCell, forRowAtIndexPath indexPath: NSIndexPath)
{
    switch indexPath.section
    {
    case 2:
        cell.name = codes2[indexPath.row].objectForKey("name") as! String

        cell.descript = ((codes2[indexPath.row].objectForKey("description") as! String).isEmpty ? "-" : codes2[indexPath.row].objectForKey("description") as! String)

    case 1:
        cell.name = codes1[indexPath.row].objectForKey("name") as! String

        cell.descript = ((codes1[indexPath.row].objectForKey("description") as! String).isEmpty ? "-" : codes1[indexPath.row].objectForKey("description") as! String)

    default:
        cell.name = codes0[indexPath.row].objectForKey("name") as! String

        cell.descript = ((codes0[indexPath.row].objectForKey("description") as! String).isEmpty ? "-" : codes0[indexPath.row].objectForKey("description") as! String)
    }
}

通过简单地查看您的代码,您似乎只需要一个UICollectionView设置就可以了,该设置使用UICollectionReusableView作为headers 这样,所有collectionViews将重用相同的cells并且每次UITableViewCell变得可见时您都不必布局collectionViews

界面生成器设置

此外,您可能会考虑code.componentsSeparatedByString(",").count等操作的结果存储在单独的变量中,并且仅在数据更改后才计算它。

暂无
暂无

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

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