Expand/Condense UICollectionView 中的所有 UICollectionView 单元格
Expand/Condense all UICollectionView Cells in a UICollectionView
我正在尝试对右上角的按钮进行编码,以便在点击时,它只会在白色栏中显示名称并且不会显示照片,或者人在卡片上的位置。这是一张说明扩展部分的照片。
我尝试使用 collectionView
中的 sizeForItemAt
来操纵大小,但这没有用。这是我所做的:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
let cell = collectionView.cellForItem(at: indexPath) as? CardCollectionViewCell
if expand {
cell?.profileimage.alpha = 0
cell?.poscomp.alpha = 0
return CGSize(width: 360, height: 208)
} else {
cell?.profileimage.alpha = 1
cell?.poscomp.alpha = 1
return CGSize(width: 360, height: 40)
}
}
对于按钮,我做了...
@IBAction func collapse(_ sender: Any) {
expand = !expand
for index in 0...associates.count {
cardCollectionView.reloadItems(at: [IndexPath(row: 0, section: index)])
}
}
是否有 correct/simpler 方法来操纵 CollectionViewCell 中的高度和元素?
提前致谢。
你的代码是对的,但我没发现你的问题。
添加以下运行良好的代码,这可能是您所期待的。
var expandedHeight : CGFloat = 200
var notExpandedHeight : CGFloat = 50
var isExpanded = false
在按钮的操作方法中添加代码以重新加载collectionView
with/without 动画
@IBAction func btnExpandTapped(_ sender: UIButton) {
self.isExpanded = !self.isExpanded
// Simple reload
// self.cvList.reloadData()
// Reload with animation
for i in 0..<5 {
let indexPath = IndexPath(item: i, section: 0)
UIView.animate(withDuration: 0.8, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.9, options: .curveEaseInOut, animations: {
self.cvList.reloadItems(at: [indexPath])
}, completion: { success in
print("success")
})
}
}
添加 UICollectionViewDelegateFlowLayout
的 sizeForItemAt
方法并添加以下代码以更改集合视图单元格的高度。
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if isExpanded {
return CGSize(width: collectionView.frame.width, height: 200)
} else {
return CGSize(width: collectionView.frame.width, height: 40)
}
}
}
检查以下 link 以获取演示应用程序。
https://freeupload.co/files/02c31165b3c6876678301ae075f5722b.zip
我正在尝试对右上角的按钮进行编码,以便在点击时,它只会在白色栏中显示名称并且不会显示照片,或者人在卡片上的位置。这是一张说明扩展部分的照片。
我尝试使用 collectionView
中的 sizeForItemAt
来操纵大小,但这没有用。这是我所做的:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize{
let cell = collectionView.cellForItem(at: indexPath) as? CardCollectionViewCell
if expand {
cell?.profileimage.alpha = 0
cell?.poscomp.alpha = 0
return CGSize(width: 360, height: 208)
} else {
cell?.profileimage.alpha = 1
cell?.poscomp.alpha = 1
return CGSize(width: 360, height: 40)
}
}
对于按钮,我做了...
@IBAction func collapse(_ sender: Any) {
expand = !expand
for index in 0...associates.count {
cardCollectionView.reloadItems(at: [IndexPath(row: 0, section: index)])
}
}
是否有 correct/simpler 方法来操纵 CollectionViewCell 中的高度和元素?
提前致谢。
你的代码是对的,但我没发现你的问题。 添加以下运行良好的代码,这可能是您所期待的。
var expandedHeight : CGFloat = 200
var notExpandedHeight : CGFloat = 50
var isExpanded = false
在按钮的操作方法中添加代码以重新加载collectionView
with/without 动画
@IBAction func btnExpandTapped(_ sender: UIButton) {
self.isExpanded = !self.isExpanded
// Simple reload
// self.cvList.reloadData()
// Reload with animation
for i in 0..<5 {
let indexPath = IndexPath(item: i, section: 0)
UIView.animate(withDuration: 0.8, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.9, options: .curveEaseInOut, animations: {
self.cvList.reloadItems(at: [indexPath])
}, completion: { success in
print("success")
})
}
}
添加 UICollectionViewDelegateFlowLayout
的 sizeForItemAt
方法并添加以下代码以更改集合视图单元格的高度。
extension ViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if isExpanded {
return CGSize(width: collectionView.frame.width, height: 200)
} else {
return CGSize(width: collectionView.frame.width, height: 40)
}
}
}
检查以下 link 以获取演示应用程序。
https://freeupload.co/files/02c31165b3c6876678301ae075f5722b.zip