【问题标题】:UIcollectionview lagging while scrolling cells滚动单元格时 UIcollectionview 滞后
【发布时间】:2017-05-07 15:56:50
【问题描述】:

我正在使用UICollectionview 创建 7 个单元格。当我滚动时,应用程序工作正常,但如果我继续滚动,它开始滞后并且阴影(每个单元格后面)变得更暗。

我认为从屏幕上消失的单元格不会被删除,当我返回时,程序会在最旧的单元格的相同位置重新创建一个新单元格。有什么解决办法吗?

滚动前的画面

https://ibb.co/dNQJ5k

滚动后的屏幕

https://ibb.co/kjDAJ5

这是代码

class menuController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

@IBOutlet weak var coll_view: UICollectionView!

var array = [String]()
override  func viewDidLoad() {
    array = ["segue_menu_map", "segue_menu_camera"]
    coll_view.scrollToItem(at: IndexPath(item: 2, section: 0), at: .left, animated: true)
}

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

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) 
    let button = UIButton(type: .custom)
    button.frame = CGRect(x: 0, y: 0, width: cell.frame.width, height: cell.frame.height)
    button.layer.cornerRadius = 0.2*button.frame.width
    button.backgroundColor = UIColor.white
    button.layer.borderWidth = 2
    button.tag=indexPath.row
    button.layer.shadowColor = UIColor.lightGray.cgColor
    button.layer.shadowOpacity = 1.0
    button.layer.shadowOffset = CGSize(width: 0.4, height: 1.8)
    cell.clipsToBounds = false
    button.addTarget(self, action: #selector(collectionAction(sender:)), for: .touchUpInside)
    print([indexPath.row])
    //cell.backgroundColor = UIColor.gray
    button.setTitle(String(indexPath.row), for: .normal)
    button.setTitleColor(.black, for: .normal)
    cell.addSubview(button)
    return cell
}



func collectionAction( sender: UIButton) {
    if sender.tag < 2{
    self.performSegue(withIdentifier: array[sender.tag], sender: nil)
    }

 }
}

提前致谢

【问题讨论】:

  • viewDidLoad 函数调用super.viewDidLoad()。此外,collectionViewCells 被重用,并且因为您是cellForRowAt 中的按钮,它可能不会从视图层次结构中删除。您是否为单元格覆盖了prepareForReuse
  • 不,我没有。我该怎么做?
  • 我强烈建议您查看这个答案:stackoverflow.com/questions/31735228/…

标签: ios swift uicollectionview cell


【解决方案1】:

collectionView.dequeueReusableCell不会每次都创建一个新单元格。它重用已创建的单元格以提高性能。您每次重复使用时都会向单元格添加一个按钮,这可能意味着单个单元格上可能有数十/数百个按钮。

解决方案是创建 UICollectionViewCell 的子类并将设置代码放在那里。

【讨论】:

  • 我创建了一个名为“buttonCollectionView”的子类,现在我必须将数据(indexpath.row)从第一类的 collectionview 方法发送到这个类。所以我在collectionview方法中设置“let delegate = buttonCollectionView()”“delegate.button.tag = indexPath.row”。现在,如果我打印 delegate.button.tag 它会显示我想要的数字,但如果我尝试在“buttonCollectionView”类中打印 button.tag,它只会显示每个按钮的 0。我该如何解决? (对不起我的英语不好)
【解决方案2】:

我在滚动 collectionView 时遇到了同样的问题。 我发现了两个主要问题并解决了这个问题。

  1. 我将创建按钮之类的代码明确地移到了 collectionviewcell .m 文件中。这给我带来了流畅的滚动。
  2. 我删除了单元格的影子属性。相信我,它会立即消除滞后。 现在我不知道如何用阴影偏移解决这个问题。但大写已修复滞后。

并在主类的自定义单元格或 cellforItem 中添加此代码:

主类

cell.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds cornerRadius:cell.contentView.layer.cornerRadius].CGPath;

用于自定义单元格

self.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds cornerRadius:cell.contentView.layer.cornerRadius].CGPath;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多