Ios 如何以编程方式触发UITableViewCell编辑操作?

Ios 如何以编程方式触发UITableViewCell编辑操作?,ios,uitableview,swift4,Ios,Uitableview,Swift4,我在tableviewcell上进行了一些自定义编辑操作。当我刷卡时,它工作正常,但我想知道当我点击手机时是否有任何方法触发这些动作。另外,我也看到很多人用同样的方式回答类似的问题 tableView.setEditing(true, animated: true) 虽然这不是我正在寻找的解决方案。我希望不用再按另一个按钮就可以立即显示动作 简单的回答是——没有这样的方法 然而,如果您真的需要类似的东西,您可以模仿这种行为,尽管它需要更多的实现和状态处理 下面是一个快速且非常脏的解决方案,它覆

我在tableviewcell上进行了一些自定义编辑操作。当我刷卡时,它工作正常,但我想知道当我点击手机时是否有任何方法触发这些动作。另外,我也看到很多人用同样的方式回答类似的问题

tableView.setEditing(true, animated: true)
虽然这不是我正在寻找的解决方案。我希望不用再按另一个按钮就可以立即显示动作


简单的回答是——没有这样的方法

然而,如果您真的需要类似的东西,您可以模仿这种行为,尽管它需要更多的实现和状态处理

下面是一个快速且非常脏的解决方案,它覆盖了自定义单元格的
touchesend
方法。请记住在相关情节提要的表视图中将单元设置为单元的动态原型,并将其重用标识符设置为
identifer

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, CellDelegate {

    @IBOutlet weak var tableView: UITableView?
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "identifier") as? Cell else {
            return UITableViewCell()
        }
        cell.textLabel?.text = "\(indexPath.row)"
        cell.indexPath = indexPath
        cell.delegate = self
        return cell
    }

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        return nil
    }

    func doAction(for cell: Cell) {
        let indexPath = cell.indexPath
        print("doing action for cell at: \(indexPath!.row)")
        // your implementation for action
        // maybe delete a cell or whatever?
        cell.hideFakeActions()

    }

}

protocol CellDelegate: class {
    func doAction(for cell: Cell)
}

class Cell: UITableViewCell {

    weak var delegate: CellDelegate?
    var indexPath: IndexPath!

    @IBOutlet weak var buttonAction1: UIButton?
    @IBOutlet weak var constraintButtonFakeActionWidth: NSLayoutConstraint?

    override func awakeFromNib() {
        self.constraintButtonFakeActionWidth?.constant = 0
    }
    override func touchesEnded(_ touches: Set<UITouch>,
                               with event: UIEvent?) {
        guard let point = touches.first?.location(in: self) else {
            return
        }
        if self.point(inside: point, with: event) {

            print("event is in cell number \(indexPath.row)")
            self.showFakeActions()

        }
    }

    func showFakeActions() {
        self.constraintButtonFakeActionWidth?.constant = 80
        UIView.animate(withDuration: 0.3) {
            self.layoutIfNeeded()
        }
    }

    func hideFakeActions() {
        self.constraintButtonFakeActionWidth?.constant = 0
        UIView.animate(withDuration: 0.3) {
            self.layoutIfNeeded()
        }
    }

    @IBAction func fakeAction() {
        delegate?.doAction(for: self)
    }
}
导入UIKit
类ViewController:UIViewController、UITableViewDataSource、UITableViewDelegate、CellDelegate{
@IBVAR表格视图:UITableView?
重写func viewDidLoad(){
super.viewDidLoad()
}
func numberOfSections(在tableView:UITableView中)->Int{
返回1
}
func tableView(tableView:UITableView,numberofrowsinssection:Int)->Int{
返回10
}
func tableView(tableView:UITableView,cellForRowAt indexath:indexPath)->UITableViewCell{
guard let cell=tableView.dequeueReusableCell(带标识符:“标识符”)作为?cell else{
返回UITableViewCell()
}
cell.textlab?.text=“\(indexPath.row)”
cell.indepath=indepath
cell.delegate=self
返回单元
}
func tableView(tableView:UITableView,EditActionsErrorWat indexPath:indexPath)->[UITableViewRowAction]{
归零
}
func doAction(用于单元格:单元格){
设indexPath=cell.indexPath
打印(“在以下位置对单元格执行操作:\(indexPath!.row)”)
//你的执行需要采取行动
//也许删除一个单元格或其他什么?
cell.hideFakeActions()
}
}
协议委托:类{
func doAction(用于单元格:单元格)
}
类别单元格:UITableViewCell{
弱变量委托:CellDelegate?
var indepath:indepath!
@IBOutlet弱var按钮操作1:UIButton?
@IBOutlet弱var约束ButtonFakeActionWidth:NSLayoutConstraint?
重写func awakeFromNib(){
self.constraintButtonFakeActionWidth?.constant=0
}
覆盖函数touchesend(utouchs:Set,
带事件:UIEvent?){
防护点=接触。第一?位置(在:自我)其他{
返回
}
if self.point(内部:点,带:事件){
打印(“事件位于单元格编号\(indexPath.row)”中)
self.showFakeActions()
}
}
func showFakeActions(){
self.constraintButtonFakeActionWidth?常数=80
UIView.animate(持续时间:0.3){
self.layoutifneed()
}
}
func hideFakeActions(){
self.constraintButtonFakeActionWidth?.constant=0
UIView.animate(持续时间:0.3){
self.layoutifneed()
}
}
@IBAction func fakeAction(){
委托?.doAction(用于:自我)
}
}
那么它是如何工作的呢?每个单元格都是一个从抽象
UIResponder
接口继承的
UIView
。您可以重写其方法,以代表分派给它们的事件自行执行操作。这是覆盖
touchesend
的第一步

看看interface builder的屏幕截图-您必须连接约束。

我还实现了委托,它为单元格的所有编辑操作返回nil,因此它们不会干扰您的解决方案

接下来,设置一个自定义委托以从单元格中获取回调。为了方便管理数据源中的数据,我还将IndexPath附加到单元格中,您必须实现数据源


请记住,这段代码缺少很多内容,比如
prepareforeuse
方法实现。另外,您可能希望在
touchesend
override中执行额外的检查,这将保证每次触摸不会触发此委托回调一次以上,并防止多次触摸。这里也没有实现禁用单元上用户交互的逻辑。而且界面需要微调(比如在动画中文本似乎被压扁)。

听起来你应该覆盖
tableView:didSelectRowAtIndexPath:
-点击单元格时会调用此方法,然后你可以调用相应的操作。苹果有自己的API来做你想做的事情,检查这个答案:但请记住,这不是推荐的,你的应用程序可能会被拒绝。@FrancescoDeliro我已经看到了这个问题的答案,我已经尝试在swift 4中使用它。当我运行它时,它会显示“NSInvalidArgumentException,SetShowingDeleteConfirmation with Arg1 Unrecogned selector”。虽然我在理解它如何回答我的问题时遇到了一些困难,但您的代码仍然有效。我可能还不够清楚,我想做的是当我点击一个单元格时显示编辑动作,比如删除或更多。谢谢,这对我来说很有效。