【问题标题】:Swift create left, right swipe with long press tableview row cell movableSwift创建左,右滑动,长按tableview行单元格可移动
【发布时间】:2019-12-30 07:24:55
【问题描述】:

在我的场景中,我正在尝试创建一个 UITableView 单元格,以便使用 tableView 单元格长按左右滑动以在特定部分内移动。在这里,我只能做拖尾和前导滑动,不知道如何在一个部分内移动单元格。

下面的前导和尾随代码

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])
}

【问题讨论】:

  • 如果签入都可以添加代表要启用左右滑动的部分
  • @Manikandan 我想启用左右滑动,还长按单元格以在部分内移动行。如果我启用拖放,我可以看到右侧的三行栏,如果 tableview 单元格但 Swipe 在那个时候不起作用。如何实现这三件事。

标签: ios swift uitableview


【解决方案1】:

a) 在表格视图上启用拖动交互 b) 设置拖放代理

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) {
 }
}

【讨论】:

  • 这成功了!但我得到“记录器 [31567:1594928] PBItemCollectionServicer 连接已断开。”在日志中
猜你喜欢
  • 2014-01-16
  • 1970-01-01
  • 2014-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多