我有一个fruit = ["Apple", "Orange", "Pear", "Kiwi"]具有Entity FOOD 的数组,并在UItableView中显示.有没有办法让一些内容无法取消.例如,我可以"Kiwi"取消删除.
有点像
let i = fruit.index(where: "Kiwi")
let 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()}
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用,因为indexPath不能采用数组类型.我怎样才能做到这一点?
我正在尝试从故事板转向以编程方式编码。我正在尝试将 barbuttonitem 添加到我的导航栏。但是,我不想使用自己的图像,我想使用默认的系统图像,例如“magnifyingglass.circle”。
这是我的代码:
let button = UIBarButtonItem.init(image: UIImage(contentsOfFile: "magnifyingglass.circle") , style: .plain, target: nil, action: nil)
navigationItem.leftBarButtonItem = button
Run Code Online (Sandbox Code Playgroud)
但是,我的导航栏中没有添加任何内容。