【发布时间】:2019-04-28 07:50:21
【问题描述】:
我正在使用UITableView 来显示几个部分,它们代表一个待办事项,每个部分都有几行,代表一个较小的待办事项:
- Finish thesis
-- Interview representatives
-- Rewrite chapter 8
-- Create cover sheet
- Clean the house
-- Do the dishes
-- Mop living room
-- Clean windows in bathroom
我已经成功实现了一种使用UITableView 的DragDelegate 和DropDelegate 重新排列行的方法。我确保一切都是可移动的(协议canMoveRowAt 返回真)。为了让您了解我是如何做到这一点的:
func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
let toDoName = toDoList[indexPath.section].toDoItem[indexPath.row].toDoName
let itemProvider = NSItemProvider(object: toDoName as NSString)
let dragItem = UIDragItem(itemProvider: itemProvider)
dragItem.localObject = item
return [dragItem]
}
在MoveRowAt() 中,我确保相应地更改数据,从sourceIndexPath 中删除数据并将其插入destinationIndexPath。这很好用。
但是,我想让部分也可拖动 - 以便按优先级对待办事项列表进行排序。例如,如果我添加一个新的待办事项,它会自动附加到底部,但也许这是需要首先完成的重要事情,所以我希望它 - 作为一个包含所有行的部分 - 被向上拖动如果需要。
我可以用同样的方法完成吗?我发现没有行协议(moveRowAt 存在,但 moveSectionAt 不存在,canMoveSectionAt 相同)。
我找到了一个函数,tableView.moveSection(section, toSection: section),我可能需要使用它。但是我将如何实现拖放并将其附加到该部分的标题?我需要使用自定义UILongPressGestureRecognizer 吗?有关如何完成此操作的任何提示或可用库?
【问题讨论】:
标签: ios swift uitableview drag-and-drop sections