在表视图中实现iOS 11的拖放。如果我不希望第一行被拖动,我假设从tableView返回一个空数组(:itemsForBeginning:)
func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
if indexPath.row == 0 {
return []
}
let dragItems = self.dragItems(forRowAt: indexPath)
print("dragging row index \(String(describing: dragItems.first?.localObject as? Int))")
return dragItems
}当您不允许用户从指定的索引路径拖动内容时,返回一个空数组。
但是,即使它被验证返回,拖动仍然发生。这意味着要么我搞砸了,要么这个特性没有按文档实现。我总是犹豫不决,不认为是别人,所以我的问题是,返回的as实现实际上应该防止拖放0行?还有其他人来验证这一点或者证明它是按照文件记录的吗?
谢谢
编辑:来自WWDC视频的示例代码包括以下内容:
if tableView.isEditing {
// User wants to reorder a row, don't return any drag items. The table view will allow a drag to begin for reordering only.
return []
}这是说,如果您不返回任何拖动项目,表视图仍将允许拖动?!!那么,如何才能防止一排人被拖走呢?
发布于 2017-08-25 23:05:37
感谢@Losiowaty指出了有用的方向。
我知道,在tableView中,如果只有一个UIDragItem,那么drop委托就会转向tableView(:moveRowAt:)。我在任何地方都没有看到有文档记录的地方是,它也检查了tableView(:canMoveRowAt:),尽管现在回想起来这一点似乎是显而易见的。
我的canMoveRowAt看起来是这样的:
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable. If coded that the first row is non-editable, that row is by definition also non-re-orderable
return true
}注意方法内部的注释。我不知道我是写了这条评论还是从某个地方抄袭过来的。我已经阻止了行0是可编辑的(在编辑模式下是可删除的和可重新排序的),并让它覆盖canMoveRowAt,但是这显然被iOS 11拖放时忽略了。因此,解决方案是明确的,如:
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
if indexPath.row == 0 {return false}
return true
}诊断这一点的另一个复杂性是,iMessage应用程序在iPad上的相同代码没有到达tableView(:moveRowAt:),而是在iPhone上到达那里。对于iOS应用程序,tableView(:moveRowAt:)与iPad和iPhone都有联系,尽管这可能是另一个问题。
https://stackoverflow.com/questions/45889033
复制相似问题