首页 > 解决方案 > 如何更改集合视图中单个单元格的大小?我想获取滑块的值并使用该值来调整单元格的大小

问题描述

到目前为止,我已经在每个单元格中创建了一个带有标签的集合视图。我有一个将新单元格附加到数组的按钮。我什至可以让单元格根据滑块的值平均改变高度。但是,我不确定如何更改集合视图中仅一个单元格的大小。任何人都知道如何做到这一点?我还没有看到它的答案。

如有必要,我可以发布我的代码

编辑:由于要求,这里是代码:

 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return visualArray.count
}


func collectionView(_: UICollectionView, layout: UICollectionViewLayout, sizeForItemAt: IndexPath) -> CGSize {

    return CGSize(width: self.collectionThing.frame.height / 6 * 5, height: CGFloat(sliderSlider.value * 4))
}



func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "VisualCollectionViewCell", for: indexPath) as! VisualCollectionViewCell

    cell.visualLabel.text = visualArray[indexPath.row]

    return cell
}

标签: iosswift

解决方案


假设您使用的UICollectionViewDelegateFlowLayout是默认布局,您可以使用委托方法func collectionView(UICollectionView, layout: UICollectionViewLayout, sizeForItemAt: IndexPath) -> CGSize为该特定单元格返回新大小。

collectionView.performBatchUpdates我相信如果您使用空闭包调用,您可以触发动画重新布局,一旦单元格的大小发生变化,您应该这样做。

如果这不起作用,您将需要创建一个自定义集合视图布局,网上有一些关于如何做到这一点的教程。


推荐阅读