swift - 分别展开每个 collectionView 单元格
问题描述
我有一个原型collectionViewCell
将被创建 4 次。它里面有一个按钮来展开单元格(类似于 iOS 上的小部件)。
我在单元类中添加了一个闭包
var expand : (() -> Void)?
@IBAction func pressExpandeBtn(_ sender: UIButton) {
expand?()
}
然后在UICollectionViewController
:
enum Style {
case collapsed, expanded
}
var style = Style.collapsed {
didSet {
collectionView.reloadData()
}
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "colorCell", for: indexPath) as? ColorViewCell {
switch indexPath.row {
case 0:
cell.expand = { [weak self] in
switch self!.style {
case .collapsed:
self!.style = .expanded
case .expanded:
self!.style = .collapsed
}
}
case 1:
cell.expand = { [weak self] in
switch self!.style {
case .collapsed:
self!.style = .expanded
case .expanded:
self!.style = .collapsed
}
}
case 2:
cell.expand = { [weak self] in
switch self!.style {
case .collapsed:
self!.style = .expanded
case .expanded:
self!.style = .collapsed
}
}
case 3:
cell.expand = { [weak self] in
switch self!.style {
case .collapsed:
self!.style = .expanded
case .expanded:
self!.style = .collapsed
}
}
default:
break
}
return cell
}
return UICollectionViewCell()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.bounds.inset(by: collectionView.layoutMargins).size.width
if indexPath.row == indexPath.row {
switch style {
case .collapsed:
return CGSize(width: width, height: 150)
case .expanded:
return CGSize(width: width, height: 400)
}
}
return CGSize(width: width, height: 150)
}
现在当我按下展开按钮时,单元格的大小会增加,但问题是,点击按钮后所有单元格的高度都会增加和减少,我不知道如何判断单元格是它的按钮应该调整大小,而不是全部。有人可以帮我吗?
太感谢了。
解决方案
问题是您为所有单元格声明了 1 个 var
var style = Style.collapsed {
didSet {
collectionView.reloadData()
}
}
所以所有单元格的行为都是一样的,但是每个单元格都应该有自己的状态,无论它是折叠还是展开,所以声明一个数组,再加上这个if indexPath.row == indexPath.row {
内部检查sizeForItemAt
对所有人都是正确的,因为变量肯定等于它自己,因此应用了样式大小所有人都一样
var arr = [.collapsed,.collapsed,.collapsed,.collapsed]
然后根据每个单元格状态更改它
cell.expand = { [weak self] in
guard let strSelf = self else { return }
strSelf.arr[indexPath.item] = strSelf.arr[indexPath.item] == .collapsed ? .expanded : .collapsed
strSelf.collectionView.reloadData()
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = collectionView.bounds.inset(by: collectionView.layoutMargins).size.width
return arr[indexPath.item] == .collapsed ? CGSize(width: width, height: 150) : CGSize(width: width, height: 400)
}
推荐阅读
- mongodb - 如何连接我在 App Engine 的计算引擎中托管的 mongoDb 也托管在项目的同一区域?
- reactjs - 如何在 React 中读取请求标头?
- ios - 当我在模型中使用 withAnimation 时,没有动画发生,为什么?
- android - FirebaseError:Firebase:名为“[DEFAULT]”的 Firebase 应用程序已存在(应用程序/重复应用程序)
- python - 如何修复此错误“无法将 numpy 数组转换为张量”
- c# - DataTemplate 中 TreeViewItem 的 IsExpanded 属性
- flutter - 扑:虫子?只有 FlatButton 而不是 CupertinoButton 显示文本
- android - 在应用程序类中访问启动意图
- python - 为什么 concurrent.futures 中的 ThreadPoolExecutor 运行时间比非并行运行时间长?
- app-store - 将 Andriod 应用上传到 APkPure 后,系统一直显示 Error 10003 In