代码之家  ›  专栏  ›  技术社区  ›  Pooja Gupta

是否可以手动调用UITableview的editationsforrowat?

  •  0
  • Pooja Gupta  · 技术社区  · 8 年前

    在我的项目中,需要调用 editActionsForRowAt 一个 UITableView 一个按钮动作。所以在点击那个按钮时, tableview 向左轻扫。

    我的编辑错误代码:

    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    
    
        let editAction = UITableViewRowAction(style: .normal, title: "Edit".localized) {[weak self] (rowAction, indexPath) in
    
            // Perform edit action here
        }
    
        let deleteAction = UITableViewRowAction(style: .normal, title: "Delete".localized) {[weak self] (rowAction, indexPath) in
             // Perform delete action here
        }
    
        return [deleteAction,editAction]
    }
    

    我不知道有没有可能?我找了很多,但没有找到任何解决办法。请提出是否可能?如果是,那么如何实现这一点?

    2 回复  |  直到 8 年前
        1
  •  1
  •   shivi_shub    8 年前
    class YourViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, CustomCellDelegate {
    
    @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 2
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "identifier") as? CustomCell else {
            return UITableViewCell()
        }
        cell.indexPath = indexPath
        cell.delegate = self
        return cell
    }
    
    func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
        return nil
    }
    
    func delete(for cell: CustomCell) {
        let indexPath = cell.indexPath
        print("doing action for cell at: \(indexPath!.row)")
        // your implementation for action
        // maybe delete a cell or whatever?
        cell.hideDeleteActions()
    
    }
    
    }
    
    protocol CustomCellDelegate: class {
    func delete(for cell: CustomCell)
    }
    
    class CustomCell: UITableViewCell {
    
    weak var delegate: CustomCellDelegate?
    var indexPath: IndexPath!
    
    @IBOutlet weak var buttonAction: UIButton?
    @IBOutlet weak var constBtnDeleteAction: NSLayoutConstraint?
    
    override func awakeFromNib() {
        self.constBtnDeleteAction?.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) {
            self.showDeleteActions()
    
        }
    }
    
    func showDeleteActions() {
        self.constBtnDeleteAction?.constant = 100
        // Set it according to the button width
        UIView.animate(withDuration: 0.3) {
            self.layoutIfNeeded()
        }
    }
    
    func hideDeleteActions() {
        self.constBtnDeleteAction?.constant = 0
        UIView.animate(withDuration: 0.3) {
            self.layoutIfNeeded()
        }
    }
    
    @IBAction func cellButtonAction() {
        delegate?.delete(for: self)
    }
    }
    

    你需要做更多像故事板一样的设置按钮的约束,它将在单元格的最右边,并加入插座。

        2
  •  0
  •   Rakesh Patel    8 年前

    我认为可以调用tableview的delegate方法,如下所示

    @IBAction func editingstylecalled(_ sender:UIButton)
    {
        let tag = sender.tag
        self.tableView(self.tableview, commit: .delete, forRowAt: IndexPath.init(row: tag, section: 0))
    }
    

    请在上面的代码中设置您的tableview并检查。