我正在尝试使用iOS 11方式在表格视图行中添加滑动操作。我想添加一个删除行的操作。
我的测试表视图显示从0到9的数字,数据源是一个简单的整数数组,称为numbers:
class ViewController: UIViewController {
var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
}
Run Code Online (Sandbox Code Playgroud)
选择一行时,我将打印以下相关值numbers:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print(numbers[indexPath.row])
tableView.deselectRow(at: indexPath, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
我实现新的委托trailingSwipeActionsConfigurationForRowAt,如下所示:
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let delete = UIContextualAction(style: .destructive, title: "Delete") { (_, _, completionHandler) in
self.numbers.remove(at: indexPath.row)
completionHandler(true)
}
return UISwipeActionsConfiguration(actions: [delete])
}
Run Code Online (Sandbox Code Playgroud)
当我在行上滑动时,删除工作正常。但是,这就是我的问题,当我在删除后选择另一行时,关联的IndexPath错误...
例:
我做错了什么?