我正在尝试创建一个UICollectionView
彼此下面的3个单元格。这工作正常。当我触摸一个单元格时,该单元格应该展开,其他单元格应该折叠到特定高度。也工作得很好。
我无法解决的问题;当我点击一个单元格时,它会像我想要的那样扩展;它与这个下面的那个重叠。下面的那个不会向下移动,以便为该单元格增加新的空间。
我怎样才能解决这个问题?
到目前为止,这是我与UICollectionView
和相关的UICollectionViewCell
/**
* 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
替代中发生。
首先,我必须正确定义具有单元配置的阵列。在这里,我添加了一个带有布尔值的键“ active”。
当我点击一个单元格时,我将'active'布尔值切换到true
objects数组内,然后在调用之后,它将在显示之前collectionView.reloadData()
执行sizeForItemAt
覆盖UICollectionView
。这样可以确保单元正确对齐并准确执行其应做的工作。
在sizeForItemAt
I中,我基于对象数组中的“活动”布尔值返回适当的高度。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句