我有一个水平滚动的 Collection View 。
我正在寻找通过向上或向下滑动手势移除项目的简洁方法。
重新排列元素也很棒,但目前移除元素更为重要。
我找到了一些 Obj-C 文档,但是,由于我对 swift Obj-C 还是个新手,所以对我来说太多了。
最佳答案
最近几天我一直在处理同样的情况。 这是我用 swift 所做的……我检查了 Michael 的链接并做了一些研究……
所以..
添加这个
let cSelector = Selector("reset:")
let UpSwipe = UISwipeGestureRecognizer(target: self, action: cSelector )
UpSwipe.direction = UISwipeGestureRecognizerDirection.Up
cell.addGestureRecognizer(UpSwipe)
到
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
然后定义您的选择器,它实际上从您的数组中删除滑动的项目,然后重新加载您的 Collection View 。
func reset(sender: UISwipeGestureRecognizer) {
let cell = sender.view as! UICollectionViewCell
let i = self.favoritesCV.indexPathForCell(cell)!.item
favoritesInstance.favoritesArray.removeAtIndex(i) //replace favoritesInstance.favoritesArray with your own array
self.favoritesCV.reloadData() // replace favoritesCV with your own collection view.
}
关于Swift CollectionView 使用滑动手势删除项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29889353/