我有一个数组 fruit = ["Apple", "Orange", "Pear", "Kiwi"]
具有实体 FOOD 并显示在 UItableView 中。有没有办法让一些内容不可删除。例如,我可以使 "Kiwi"
不可删除吗?
有点像,
让 i = fruit.index(其中:“猕猴桃”)
让 IArr = [0...fruit.count]
IArr = IArr.filter{$0 != i}
//删除Kiwi的索引
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IArr) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
if editingStyle == .delete{
let FRUIT = fruit[indexPath.row]
context.delete(FRUIT)
appDelegate.saveContext()
do {
fruit = try context.fetch(FOOD.fetchRequest())
}
catch
{
print("did not fetch")}
}
tableView.reloadData()}
但是,这不起作用,因为 indexPath
不能采用数组类型。
我该怎么做?
最佳答案
您可以测试索引路径处的行不是 Kiwi:
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IArr) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext {
let FRUIT = fruit[indexPath.row]
if editingStyle == .delete && FRUIT != "Kiwi" {
/* delete */
}
}
关于ios - 只在 UITableView 中删除一些行 [Swift 3.0 - Xcode 8],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45361310/