gpt4 book ai didi

swift - Collectionview Reloaddata 函数不工作

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

我有一个 collectionView,我在其中显示带有 UIButton 的普通 UICollectionViewCell。在另一个屏幕上,我有一个开关列表。每当这些更改时,就会使用 collectionView 将更新发送到屏幕。当我关闭一个开关(并且更新了数据源)时,我尝试使用 reloadData 函数重新填充 collectionView。但是,按钮(单元格)显示在彼此之上。

我尝试使用 reloadData,在 Dispatch.main.async 中执行它,使 collectionView 无效,删除 deleteSections 函数并访问特定单元格并使用 cell.removeFromSuperView() 将其删除。

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "identifier", for: indexPath)
let name = PickingState.getSelectedFilterValues()[indexPath.row]
let button = SelectionsItem(title: name, frame: createMockupButtonToGetFrame(name: name))

print(name)

cell.contentView.addSubview(button)
cell.contentView.addConstraintsWithFormat(formatString: "H:|[v0]|", views: button)
cell.contentView.addConstraintsWithFormat(formatString: "V:|[v0]|", views: button)

return cell
}

期望单元格重新渲染而不是彼此重叠。

最佳答案

问题就在这里

cell.contentView.addSubview(button)

由于单元格出列,您需要在添加之前清除所有按钮

cell.contentView.forEach { 
if $0.tag == 22 {
$0.removeFromSuperview()
}
}
let button = SelectionsItem(title: name, frame: createMockupButtonToGetFrame(name: name))
button.tag = 22
cell.contentView.addSubview(button)

也别忘了

button.translatesAutoresizingMaskIntoConstraints = false

关于swift - Collectionview Reloaddata 函数不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56890053/

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