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



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

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

编辑:自要求以来,这是代码:

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
}

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

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

如果这不起作用,则需要创建自定义集合视图布局,网上有一些关于如何执行此操作的教程。

最新更新