tableViewCell 可以同时可移动并具有 .none 编辑风格的滑动操作吗

BCz*_*Cza 2 uitableview ios swift

我正在尝试制作一个符合以下标准的标准 UITableView:

1)单元格需要始终可移动,右侧有汉堡包图标

2)单元格需要滑动动作。

3)单元格的左侧不能有默认的iOS删除图标(带(-)的红色小圆圈)

我尝试过一个示例项目,并为表实现了以下代码

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var myTableView : UITableView!

var myArray = ["one","two","three","four","five"]

override func viewDidLoad() {
    super.viewDidLoad()

    self.myTableView.delegate = self
    self.myTableView.dataSource = self

    myTableView?.register(myTableCell.nib, forCellReuseIdentifier: myTableCell.identifier)

    myTableView.isEditing = true

}
}

extension ViewController : UITableViewDelegate, UITableViewDataSource{

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return myArray.count 
}
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
     return true
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle { 
     return .none
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: myTableCell.identifier, for: indexPath) as? myTableCell else {
        return myTableCell()
    }
    cell.label.text = myArray[indexPath.row]
    cell.showsReorderControl = true
    return cell
}

func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
    let rowToMove = myArray[sourceIndexPath.row]
    myArray.remove(at: sourceIndexPath.row)
    myArray.insert(rowToMove, at: destinationIndexPath.row)
}

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let deleteAction : UITableViewRowAction = UITableViewRowAction(style: .destructive, title: "Delete", handler: { (action,indexPath) -> Void in
        self.myArray.remove(at: indexPath.row)
        self.myTableView.deleteRows(at: [indexPath], with: .fade)
    })
    return [deleteAction]
}
}
Run Code Online (Sandbox Code Playgroud)

如果我注释掉另一个功能,我可以让滑动删除或移动代码起作用,但我很好奇是否可以同时获得两者。

谢谢

AVS*_*AVS 5

我今天刚刚遇到这个问题。这是我学到的: UITableView 有一个 .editing 属性 - 这是切换开/关滑动操作和移动单元格的关键。

当 tableView.editing 为 true 时 -> 那么 tableView: didSelectRowAtIndexPath: 和滑动操作将被禁用,而 UITableViewCell 可以“编辑”(例如移动)。

当 tableView.editing 为 false -> 那么 tableView: didSelectRowAtIndexPath: 和滑动操作起作用,并且 UITableViewCell 无法“编辑”(例如,它们无法拖动/移动)。

不幸的是,如果不深入挖掘,您的条件似乎是相互排斥的。我设想的最好的简单解决方案是通过按钮或手势(可能长按 UITableView)切换 UITableView 的 .editing 状态。

(也许这应该是一条评论 - 我没有足够的积分/业力来写评论 - 抱歉,如果我违反了协议,我只是想变得有用!)