我有一个名称表,我正在为它们创建一个滑动和删除函数,该函数将它们从一个名变量(数组)中删除.

我 Select 了与xcode中的教程最相似的函数并填写了它们,但当我点击删除按钮时,我的应用程序随机崩溃.以下是我的删除按钮代码:

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let deleteAction = UITableViewRowAction(style: .destructive, title: "Delete") { (rowAction: UITableViewRowAction, indexPath: IndexPath) -> Void in
        print("Deleted")
        self.catNames.remove(at: indexPath.row)
        self.tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic)
        self.tableView.reloadData()
    }
}

当我在学习swift 2和swift 3的新教程时,我会继续学习.

推荐答案

Swift 3Swift 4工作

使用UITableViewDataSource tableView(:commit:forRowAt:)方法,另请参见答案here:

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

    self.catNames.remove(at: indexPath.row)
    self.tableView.deleteRows(at: [indexPath], with: .automatic)
  }
}

Swift相关问答推荐

TabView中的视频播放器意外播放

如何在HStack中均匀分布视图?

音频播放器无法播放URL音频(操作系统状态错误2003334207.)

是否可以将具有关联值的枚举强制转换为其类型与关联值匹配的变量?

如何使用AsyncTimerSequence获取初始时钟,然后在指定的时间间隔内开始迭代?

协议元类型'SomeProtocol.Type'上无法使用静态成员'currency'

除法中如果除以完全因数需要更长时间吗?

设备上ScrollView为什么会将内容高度更改为随机值,而在预览上不会? - 优化后的标题:设备上ScrollView内容高度随机变化问题解决

是否可以利用 Codable 从 Dictionary 初始化符合类型

子视图未在父视图 SwiftUI 中触发禁用状态更新

在领域 SwiftUI 异常中使用 Apple 登录:无法识别的 Select 器发送到实例

SwiftUI View .tint(_ Color) 方法不起作用

当使用 CGFloat 而不是数字文字时,为什么 node 的位置会发生变化?

不使用 ViewController 时如何显示推送通知的alert 对话框

获取 SwiftUI 中 Binding 的值更改更新

找不到目标AAA的 SPM 工件 - 仅限 Xcode 13.3

在 Swift 4 中实现自定义解码器

无法使用锚激活约束

即使设置为从不也可以访问 iOS11 照片库

判断用户是否登录到 iCloud?Swift /iOS