解决方法如下所示:
// 1. 定义一个布尔变量,用于标记是否需要删除某些行
var shouldDeleteRows = false
// 2. 在删除行之前,将shouldDeleteRows设置为true,表示需要删除某些行
shouldDeleteRows = true
// 3. 在UITableView的删除行方法中添加判断,如果shouldDeleteRows为true,则不删除某些行
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
if shouldDeleteRows {
// 不删除某些行的操作
return
}
// 删除行的操作
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
在上述代码中,我们通过一个布尔变量shouldDeleteRows
来标记是否需要删除某些行。在删除行之前,将shouldDeleteRows
设置为true
,表示需要删除某些行。然后,在UITableView的删除行方法中,添加一个判断,如果shouldDeleteRows
为true
,则不执行删除行的操作,以达到不删除某些行的效果。需要注意的是,为了实现这个功能,在其他地方需要将shouldDeleteRows
重新设置为false
,否则在其他情况下仍然会删除行。