gpt4 book ai didi

ios - swift 3 : Resizing UICollectionViewCell programmatically causes cells to overlap

转载 作者:可可西里 更新时间:2023-11-01 00:58:36 24 4
gpt4 key购买 nike

我正在尝试创建一个 UICollectionView,其中 3 个单元格彼此下方。这工作正常。当我触摸一个单元格时,这个单元格应该展开,其他单元格应该折叠到特定高度。也工作得很好。

我无法解决的问题;当我点击一个单元格时,它会像我想要的那样展开;它与下面的重叠。下面的那个不会向下移动来为这个单元格的新高度腾出空间。

我该如何解决这个问题?

这是我到目前为止与 UICollectionViewUICollectionViewCell 相关的内容

/**
* Tempory 3 items
*/
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}

/**
* Defining the cells
*/
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! LicenseplateCollectionViewCell

cell.backgroundColor = .white
cell.layer.cornerRadius = 4

if(indexPath.row > 0){
cell.carStack.isHidden = true
}

return cell
}


/**
* Initial size
*/
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
// The first cell always start expanded with an height of 300
if(indexPath.row == 0){
return CGSize(width: collectionView.bounds.size.width, height: 300)
}else{
// The others start with an height of 80
return CGSize(width: collectionView.bounds.size.width, height: 80)
}
}

/**
* This functio shoud be handling everything like an accordeon system.
* When I touch one cell, the others should collapse.
*/
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

// Animate resize of cell after tapping
UIView.animate(withDuration: 0.2, animations: {
// For now I have 3 cells, which I will loop
for i in 0...2{
// If this is the active cell (I just tapped on) ...
if(indexPath.row == i){
let cell = collectionView.cellForItem(at: indexPath) as! LicenseplateCollectionViewCell
// This cell is expanded already, collapse it
if(cell.frame.size.height > 200){
cell.carStack.isHidden = true
cell.frame.size = CGSize(width: collectionView.bounds.size.width, height: 80)
}else{
// Open up this cell
cell.carStack.isHidden = false
cell.frame.size = CGSize(width: collectionView.bounds.size.width, height: 300)
}
}else{
// Other cells besides the one I just tapped, will be collapsed
print(indexPath.row, indexPath.section)
let customPath = IndexPath(row: i, section: 0)
print(customPath.row, customPath.section)
let cell = collectionView.cellForItem(at: customPath) as! LicenseplateCollectionViewCell
cell.carStack.isHidden = true
cell.frame.size = CGSize(width: collectionView.bounds.size.width, height: 80)
}
}
})

}

最佳答案

我已经解决了这个问题。动画/调整大小不应发生在 didSelectItemAt 覆盖中。

首先,我必须正确定义带有单元格配置的数组。在这里,我添加了一个带有 bool 值的键“active”。

当我点击一个单元格时,我将对象数组中的“事件” bool 值切换为 true,然后调用 collectionView.reloadData(),这将执行在显示 UICollectionView 之前覆盖 sizeForItemAt。这可确保单元格正确对齐并准确执行它们应执行的操作。

sizeForItemAt 中,我根据对象数组中的“事件” bool 值返回正确的高度。

关于ios - swift 3 : Resizing UICollectionViewCell programmatically causes cells to overlap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39058301/

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