【发布时间】:2016-06-25 13:21:05
【问题描述】:
以前,我尝试用 UITableView 中的长按拖动来替换标准的苹果重新排序控件(从右侧的句柄拖动单元格)。但是,由于某种原因,通过长按拖动,我无法将单元格移动到其中已经没有单元格的部分。现在我正在尝试实现一个功能,用户可以在 UICollectionViewController 而不是 UITableView 的两个部分之间拖动单元格。我实现了长按拖动功能,但由于某种原因我遇到了同样的问题。我将如何在这些部分中添加一个虚拟单元格,以便它们永远不会为空,或者有没有更好的方法解决这个问题?还有没有不用长按就能拖动单元格的方法?
这些是我添加到我的 UICollectionViewController 类以启用长按拖动的功能:
override func viewDidLoad() {
super.viewDidLoad()
let longPressGesture = UILongPressGestureRecognizer(target: self, action: "handleLongGesture:")
self.collectionView!.addGestureRecognizer(longPressGesture)
}
func handleLongGesture(gesture: UILongPressGestureRecognizer) {
switch(gesture.state) {
case UIGestureRecognizerState.Began:
guard let selectedIndexPath = self.collectionView!.indexPathForItemAtPoint(gesture.locationInView(self.collectionView)) else {
break
}
collectionView!.beginInteractiveMovementForItemAtIndexPath(selectedIndexPath)
case UIGestureRecognizerState.Changed:
collectionView!.updateInteractiveMovementTargetPosition(gesture.locationInView(gesture.view!))
case UIGestureRecognizerState.Ended:
collectionView!.endInteractiveMovement()
default:
collectionView!.cancelInteractiveMovement()
}
}
override func collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) {
let fromRow = sourceIndexPath.row
let toRow = destinationIndexPath.row
let fromSection = sourceIndexPath.section
let toSection = destinationIndexPath.section
var item: Item
if fromSection == 0 {
item = section1Items[fromRow]
section1Items.removeAtIndex(fromRow)
} else {
item = section2Items[sourceIndexPath.row]
section2Items.removeAtIndex(fromRow)
}
if toSection == 0 {
section1Items.insert(score, atIndex: toRow)
} else {
section2Items.insert(score, atIndex: toRow)
}
}
override func collectionView(collectionView: UICollectionView, canMoveItemAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
谢谢
【问题讨论】:
-
你有没有想过这个问题?
-
@Weston 我最终使用了这个 cocoapod github.com/mmick66/KDDragAndDropCollectionView
标签: ios swift uitableview drag-and-drop uicollectionview