从 UICollectionView 中删除单元格的最常见方法是什么 - Swift

fs_*_*gre 5 ios uicollectionview swift

从 中删除单元格的最常见方法是什么UICollectionView

UITableView我使用editActionsForRowAteditingStyle方法删除行中,是否UIControllerView有类似的东西或者您需要实现自己的删除方法?

我拥有的是UICollectionView很多照片,其中每个单元格/照片都附加到一个segue,这会将您带到照片的更大版本。

最简单的方法是在方法中执行此操作didSelectItemAt,但在我的情况下,这不是一个选项,因为一旦点击照片,它就会转到另一个 viewController (较大的图像)。

在我上面描述的情况下添加删除功能的最佳方法是什么?

以下主题展示了如何使用didSelectItemAt.

如何在 Swift 中向 Collection View Cell 添加删除按钮?
如何从集合视图中删除项目?

代码

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return fruits.count
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "fruitCustomCell", for: indexPath) as! fruitCollectionViewCell

    cell.labelFruitName.text = fruits[indexPath.row].fruitName
    return cell
}
Run Code Online (Sandbox Code Playgroud)

应用图

在此输入图像描述