首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >快速创建左,右滑动长按压表视图行单元格可移动

快速创建左,右滑动长按压表视图行单元格可移动
EN

Stack Overflow用户
提问于 2019-12-30 07:24:55
回答 1查看 511关注 0票数 1

在我的场景中,我正在尝试创建一个UITableView单元格,使用tableView单元长按向左和右滑动,以便在特定区域内移动。在这里,我只能做跟踪和引导滑动,不知道如何移动一个区段内的单元格。

:用于引导和跟踪的代码

代码语言:javascript
复制
func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let deleteAction = UIContextualAction(style: .normal, title:  "Delete", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
            print("OK, marked as Delete")
            success(true)
        })
        if #available(iOS 13.0, *) {
            deleteAction.image = UIImage(systemName: "delete")
        } else {
            // Fallback on earlier versions
            deleteAction.image = UIImage(named: "delete")
        }
        deleteAction.backgroundColor = .red
        return UISwipeActionsConfiguration(actions: [deleteAction])
}

func tableView(_ tableView: UITableView,trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let modifyAction = UIContextualAction(style: .normal, title:  "Edit", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
            print("Update action ...")
            self.showaddMilestone()
            success(true)
        })
        modifyAction.image = UIImage(named: "edit")
        modifyAction.backgroundColor = .green
        return UISwipeActionsConfiguration(actions: [modifyAction])
}
EN

回答 1

Stack Overflow用户

发布于 2019-12-30 07:40:53

( a)启用表视图上的拖动交互( b)设置拖放委托

代码语言:javascript
复制
tableView.dragInteractionEnabled = true
tableView.dragDelegate = self
tableView.dropDelegate = self

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: 
 IndexPath, to destinationIndexPath: IndexPath) { }

extension TableView: UITableViewDropDelegate,UITableViewDragDelegate {

   func tableView(_ tableView: UITableView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
    return [UIDragItem(itemProvider: NSItemProvider())]
   }

   func tableView(_ tableView: UITableView, dropSessionDidUpdate session: 
   UIDropSession, withDestinationIndexPath destinationIndexPath: IndexPath?) 
   -> UITableViewDropProposal {

     if session.localDragSession != nil { 
        return UITableViewDropProposal(operation: .move, intent: .insertAtDestinationIndexPath)
    }

    return UITableViewDropProposal(operation: .cancel, intent: .unspecified)
  }

func tableView(_ tableView: UITableView, performDropWith coordinator: 
 UITableViewDropCoordinator) {
 }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59527126

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档