简体   繁体   English

滑动删除是删除tableView行的唯一方法吗?

[英]Is swipe to delete the only way of deleting a tableView row?

I've been testing an app with people and after I tell them they can swipe to delete they find it intuitive but up front not everyone - in my experience at least - is savvy enough to figure it out. 我一直在与人一起测试一个应用程序,当我告诉他们他们可以滑动删除它们后,他们发现它很直观,但并不是每个人-至少以我的经验来看-都不足够聪明。

Is there an alternative? 还有其他选择吗? I think ideally i'd like to have a little trash can or "x" on the the tableView cell that can be pressed to delete. 我认为理想情况下,我希望在tableView单元格上有一个小的垃圾桶或“ x”,可以按一下将其删除。 But not sure that is easily implemented. 但不确定是否容易实现。

The first issue I encountered is I don't think I can drag an IBOutlet from a TableViewCell to the UIViewController where I have my table view. 我遇到的第一个问题是,我认为我不能将IBOutletTableViewCell拖到具有表视图的UIViewController上。

And even if that is possible not sure how I would implement the below function when the delete button is clicked. 即使那是可能的,也不确定在单击删除按钮时如何实现以下功能。

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {

So was just wondering if swipe to delete is my only option? 因此,我只是想知道是否要刷卡删除才是我唯一的选择?

Thanks. 谢谢。

What you are describing is usually achieved by putting an 'Edit' button into one side of the Navigation Bar. 您所描述的内容通常是通过在导航栏的一侧放置“编辑”按钮来实现的。 The button puts the table into an edit mode that allows tapping of small, red delete buttons. 该按钮使表格进入编辑模式,该模式允许点击红色的小删除按钮。 Just another way to do the same thing, ie delete a row. 做相同事情的另一种方法,即删除一行。 Create a Master-Detail app from the iOS template and see how the button is created programmatically in the viewDidLoad method. 从iOS模板创建一个Master-Detail应用,然后在viewDidLoad方法中查看如何以编程方式创建按钮。 Then look at the following methods that handle the deletion, whether initiated by a swipe or the Edit button. 然后查看以下处理删除的方法,无论是通过滑动还是“编辑”按钮启动。

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
    // Return false if you do not want the specified item to be editable.
    return true
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        objects.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .fade)
    } else if editingStyle == .insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
    }
}

You can do this for implementing the "x" button on the the tableView cell: 您可以执行此操作以在tableView单元上实现“ x”按钮:

protocol CustomCellDelegate {

    func removeButtonTappedOnCell(with indexPath: IndexPath)
}

class CustomCell: UITableViewCell {

    var delegate: CustomCellDelegate?

    @IBOutlet weak var removeButton: UIButton!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

    @IBAction func removeButtonTapped(_ sender: UIButton) {
        let indexPath = IndexPath(row: sender.tag, section: 0)
        delegate?.removeButtonTappedOnCell(with: indexPath)
    }
}

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, CustomCellDelegate {

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomCell
        cell.removeButton.tag = indexPath.row
        cell.delegate = self
        return cell
    }

    func removeButtonTappedOnCell(with indexPath: IndexPath) {
        modelArray.remove(at: indexPath.row)
        tableView.deleteRows(at: [indexPath], with: .right)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM