Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 分别展开每个collectionView单元格_Swift_Uicollectionview - Fatal编程技术网

Swift 分别展开每个collectionView单元格

Swift 分别展开每个collectionView单元格,swift,uicollectionview,Swift,Uicollectionview,我有一个原型collectionViewCell,将创建4次。它里面有一个按钮来扩展单元格(类似于iOS上的小部件) 我给cell类添加了一个闭包 var expand : (() -> Void)? @IBAction func pressExpandeBtn(_ sender: UIButton) { expand?() } 然后在UICollectionViewController中: enum Style { case collapsed, expanded }

我有一个原型
collectionViewCell
,将创建4次。它里面有一个按钮来扩展单元格(类似于iOS上的小部件)

我给cell类添加了一个闭包

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()
  }
}
因此,所有单元格的行为都是相同的,但是每个单元格都应该有自己的状态,不管它是否折叠/展开,所以声明一个数组,如果indexath.row==indexPath.row{inside
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) 
}