禁用特定节上的拖动交互



如果tableView.dragInteractionEnabled = true

现在所有的表单元格都可以拖动,但我想拖动这个表中的特定行。有可能吗?

您可以使用canMoveRowAt方法本地允许/不允许移动单元格:

func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return yourDataSource[indexPath.row] is YourMovableCellClass
}

您可以在tableView(_:itemsForBeginning:at:(中返回一个空数组

返回值
表示指定行内容的UIDragItem对象数组。如果不希望用户拖动指定的行,则返回一个空数组。

https://developer.apple.com/documentation/uikit/uitableviewdragdelegate/2897492-tableview

在我的情况下(这是一个集合视图,但方法是一样的(,我检查单元格的类型,并根据它允许/禁止拖动:

func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
if let cell = collectionView.cellForItem(at: indexPath) as? CalendarCollectionCell {
let item = cell.calendar!
let itemProvider = NSItemProvider(object: item.name! as NSString)
let dragItem = UIDragItem(itemProvider: itemProvider)
dragItem.localObject = item
return [dragItem]
} else {
return [UIDragItem]()
}
}

是的,这是可能的。首先,您需要定义哪些单元格是可拖动的。

在您的tableView(cellForRowAt…(方法中设置某种条件检查。

// Some code to set up your cell
// All cells are disabled by default
// Only enable the one you want to drag.
cell.userInteractionEnabled = false
// Use this conditional to determine which cells
// become dragable.     
if indexPath.row % 2 = 0 {
// This is the cell you want to be able to drag.
cell.userInteractionEnabled = true
}
return cell

最新更新