我有以下布局:ios - TableView单元格具有collectionView性能问题-LMLPHP
滚动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设置就可以了。这样,所有的collectionViews都将重用相同的cells,而不必在每次看到collectionViews时都对UITableViewCell进行布局。

ios - TableView单元格具有collectionView性能问题-LMLPHP

您也可以考虑使用将操作的结果(例如code.componentsSeparatedByString(",").count)存储在单独的变量中,并且仅在数据更改后才进行计算。

10-08 05:35