首页 > 解决方案 > 分别展开每个 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)
}

现在当我按下展开按钮时,单元格的大小会增加,但问题是,点击按钮后所有单元格的高度都会增加和减少,我不知道如何判断单元格是它的按钮应该调整大小,而不是全部。有人可以帮我吗?

太感谢了。

标签: swiftuicollectionview

解决方案


问题是您为所有单元格声明了 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) 
}

推荐阅读