我有以下布局: 当滚动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)
}
}