gpt4 book ai didi

swift - UICollectionView.setCollectionViewLayout 出现 EXC_BAD_ACCESS 错误

转载 作者:行者123 更新时间:2023-11-28 07:53:13 25 4
gpt4 key购买 nike

这个把我难住了。

func updateCollectionView() {
DispatchQueue.main.async { [weak self] in
if let strongSelf = self {
let layout = UICollectionViewFlowLayout()
let minimumSpacing: CGFloat = 8
layout.minimumInteritemSpacing = minimumSpacing
layout.minimumLineSpacing = minimumSpacing
let dim = strongSelf.view.frame.width / CGFloat(strongSelf.size) - minimumSpacing
layout.itemSize = CGSize(width: dim, height: dim)
strongSelf.collectionView.setCollectionViewLayout(layout, animated: true)
strongSelf.collectionView.reloadData()
}
}
}

当它尝试设置 CollectionViewLayout 时,我得到:

error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x800000000)

collectionView、strongSelf 和 layout 都是有效的对象。这也仅在 strongSelf.size 小于 4 时发生(如果有帮助的话)。

最佳答案

嗯,这个对我来说很奇怪,但我用这位先生的帖子解决了它:

http://jaymayu.com/uicollectionview-and-dynamically-changing-layouts-on-runtime/

基本上,我必须这样做:

strongSelf.gridCollectionView.reloadData()
strongSelf.gridCollectionView.collectionViewLayout.invalidateLayout()
strongSelf.gridCollectionView.setCollectionViewLayout(layout, animated: false)

重要的是 reloadData() 需要在 setCollectionViewLayout 之前。我需要仔细考虑这是为什么,但这解决了问题。

关于swift - UICollectionView.setCollectionViewLayout 出现 EXC_BAD_ACCESS 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49122939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com