【发布时间】:2020-10-14 09:03:05
【问题描述】:
在 UICollectionView 中,我想在点击项目时显示/隐藏更多内容。
目前,我通过在情节提要上设计一个更大的单元格来做到这一点,我希望始终在顶部显示 UILabel。当一个项目被点击时,didSelectItemAt() 和 sizeForItemAt() 调用编码为:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
selectedIndex = indexPath.item
print("didSelectItemAt: \(selectedIndex)")
collectionView.reloadItems(at: [indexPath])
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
var size = CGSize()
size.width = collectionView.frame.width
size.height = 50
if let index = selectedIndex {
print("SizeForItemAt item:\(indexPath.item), \(index)")
if index == indexPath.item {
size.height = 150
} else {
size.height = 50
}
}
return size
}
有这个输出(取自 iPhone 模拟器屏幕截图,转换为 GIF)。请注意,当隐藏/减小较高项目的高度时,蓝色框会在较低项目后面显示动画。
有没有更好的方法来实现这个?
【问题讨论】:
标签: ios swift uicollectionview