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

标签 ios swift uitableview

我一直在与人们一起测试一款应用程序,在我告诉他们可以滑动删除后,他们发现它很直观,但并不是每个人——至少以我的经验来看——都足够精明地弄明白了。

有替代方案吗?我认为理想情况下,我希望在 tableView 单元格上有一个小垃圾桶或“x”,可以按下以删除。但不确定这是否容易实现。

我遇到的第一个问题是我认为我无法将 IBOutletTableViewCell 拖到我有我的 UIViewController表格 View 。

即使这是可能的,也不确定在单击删除按钮时我将如何实现以下功能。

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

所以只是想知道是否滑动删除是我唯一的选择?

谢谢。

最佳答案

您可以执行此操作以在 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)
    }
}

关于ios - 滑动删除是删除 tableView 行的唯一方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47967515/

相关文章:

ios - 使用标签创建 UIImageView。 swift 3

ios - 我怎样才能从一个 NSDictionary 调用 objectForKey 与一个键是 NSNumbers 的 int

ios - Swift:TitleForHeaderInSection 执行时出现问题

ios - 动态表格单元格高度(内容截断)

ios - 如何从 INDEXPATH.ROW 的属性设置标题部分的标题

iphone - 如何在 Objective C 中正确初始化子类?

ios - 如何在 objective-c 中的NSMutablearray中存储字符串变量

swift - SpriteKit - 节点不添加到 SKCameraNode - Swift

ios - backBarButtonItem 无法隐藏

php - 快速以肖像模式拍摄照片并上传到服务器 - 上传的图像显示方向错误