ios - 只在 UITableView 中删除一些行 [Swift 3.0 - Xcode 8]

标签 ios swift uitableview core-data delete-row

我有一个数组 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/

相关文章:

ios - 在模态视图 Controller 上呈现整个屏幕 View Controller

ios - Swift 4 尝试呈现其 View 不在窗口层次结构中的 ViewController

ios - 如何在 UITableView 中实现 rowspan

ios - didSelectRowAtIndexPath 中的 NSIndexPath 和 objectAtIndex 崩溃?

ios - 单个 IB 约束的简单编程修改不更新

ios - 从 UICollectionViewCell xib 转到 UIViewController

ios - 使用Core Data保存数据时出现故障

ios - PFQuery includeKey 似乎不起作用 : Key data unavailible

ios - 如何在 swift 中实现类似 Dropbox 的进度 View ?

ios - 无法识别的选择器发送到实例(步进器)