Ios Swift:无法长按将单元格拖到UICollectionViewController和UITableView中的空区域
以前,我尝试在UITableView中用长按拖动替换标准的apple重新排序控件(从右侧的控制柄拖动单元格)。然而,由于某种原因,我无法将单元格移动到一个已经没有单元格的区域。现在,我尝试实现一个功能,用户可以在UICollectionViewController(而不是UITableView)中的两个部分之间拖动单元格。我实现了长按拖动功能,但出于某种原因,我遇到了相同的问题。我该如何向这些部分添加一个虚拟单元,使它们永远不会为空,或者有更好的方法解决这个问题?还有没有一种不用长按就能拖动单元格的方法 以下是我添加到UICollectionViewController类以启用长按拖动的函数:Ios Swift:无法长按将单元格拖到UICollectionViewController和UITableView中的空区域,ios,swift,uitableview,drag-and-drop,uicollectionview,Ios,Swift,Uitableview,Drag And Drop,Uicollectionview,以前,我尝试在UITableView中用长按拖动替换标准的apple重新排序控件(从右侧的控制柄拖动单元格)。然而,由于某种原因,我无法将单元格移动到一个已经没有单元格的区域。现在,我尝试实现一个功能,用户可以在UICollectionViewController(而不是UITableView)中的两个部分之间拖动单元格。我实现了长按拖动功能,但出于某种原因,我遇到了相同的问题。我该如何向这些部分添加一个虚拟单元,使它们永远不会为空,或者有更好的方法解决这个问题?还有没有一种不用长按就能拖动单元
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
}
谢谢要先回答问题的第二部分,请使用UIPangestureRecograiser而不是UILongPressRecograiser 关于空截面,可以向空截面添加一个不可见的虚拟单元。在故事板中创建一个没有子视图的原型单元,并确保其背景颜色与集合视图相同 如果部分为空,则需要安排显示此单元格。但是,当移动开始于只有1个单元格的部分时,还需要添加一个虚拟单元格,否则该部分将在移动过程中崩溃,用户无法将单元格移回其开始的部分 在手势处理程序中,开始移动时添加临时单元格。如果尚未删除单元格,则在拖动完成时也将其删除(如果单元格未实际移动,则不会调用委托moveItemAtIndexPath方法): 在
collectionView(collectionView:UICollectionView,numberofitemsinssection:Int)
中,始终返回比模型中的项数多1的值,或者如果添加了临时虚拟单元格,则返回一个额外的单元格
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
// special case: about to move a cell out of a section with only 1 item
// make sure to leave a dummy cell
if section == temporaryDummyCellPath?.section {
return 2
}
// always keep one item in each section for the dummy cell
return max(model.numberOfPagesInSection(section), 1)
}
在collectionView(collectionView:UICollectionView,cellForItemAtIndexPath indexPath:NSIndexPath)中,如果行等于该节模型中的项数,则分配虚拟单元格
在collectionView(collectionView:UICollectionView,shouldSelectItemAtIndexPath indexPath:NSIndexPath)中禁用对单元格的选择
,方法是为虚拟单元格返回false,为其他单元格返回true
类似地,在collectionView(collectionView:UICollectionView,canMoveItemAtIndexPath-indexPath:NSIndexPath)中禁用单元格的移动。
确保目标移动路径仅限于模型中的行:
func collectionView(collectionView: UICollectionView, targetIndexPathForMoveFromItemAtIndexPath originalIndexPath: NSIndexPath, toProposedIndexPath proposedIndexPath: NSIndexPath) -> NSIndexPath
{
let proposedSection = proposedIndexPath.section
if model.numberOfPagesInSection(proposedSection) == 0 {
return NSIndexPath(forRow: 0, inSection: proposedSection)
} else {
return proposedIndexPath
}
}
现在你需要处理最后一步。更新模型,然后根据需要添加或删除虚拟单元:
func collectionView(collectionView: UICollectionView, moveItemAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath)
{
// move the page in the model
model.movePage(sourceIndexPath.section, fromPage: sourceIndexPath.row, toSection: destinationIndexPath.section, toPage: destinationIndexPath.row)
collectionView.performBatchUpdates({
// if original section is left with no pages, add a dummy cell or keep already added dummy cell
if self.model.numberOfPagesInSection(sourceIndexPath.section) == 0 {
if self.temporaryDummyCellPath == nil {
let dummyPath = NSIndexPath(forRow: 0, inSection: sourceIndexPath.section)
collectionView.insertItemsAtIndexPaths([dummyPath])
} else {
// just keep the temporary dummy we already created
self.temporaryDummyCellPath = nil
}
}
// if new section previously had no pages remove the dummy cell
if self.model.numberOfPagesInSection(destinationIndexPath.section) == 1 {
let dummyPath = NSIndexPath(forRow: 0, inSection: destinationIndexPath.section)
collectionView.deleteItemsAtIndexPaths([dummyPath])
}
}, completion: nil)
}
最后,确保虚拟单元格没有可访问性项目,以便在“画外音”打开时跳过它。Details
- Xcode 10.2(10E125),Swift 5,来自iOS 11
主意
我想建议你处理
func collectionView(_ collectionView: UICollectionView, dragSessionWillBegin session: UIDragSession) {...}
func collectionView(_ collectionView: UICollectionView, dragSessionDidEnd session: UIDragSession) {...}
开始拖动时,可以将临时单元格添加到空(或全部)部分的末尾。您的空部分将不会为空)。然后,您可以使用标准UIKITAPI删除您的单元格。在拖动结束之前,您可以删除临时单元格
为什么?
我不建议使用手势处理程序,因为:
“制造自行车”的大好机会
解决方案可能非常耗时,维护起来也很困难/昂贵
用于处理已测试的表/集合的标准API
样品
更多信息
你发现了吗?@Weston我最后用了这个椰子荚
func collectionView(_ collectionView: UICollectionView, dragSessionWillBegin session: UIDragSession) {...}
func collectionView(_ collectionView: UICollectionView, dragSessionDidEnd session: UIDragSession) {...}