Removing only some lines in UITableView [Swift 3.0 - Xcode 8]

I have an array fruit = ["Apple", "Orange", "Pear", "Kiwi"]

that has an Entity FOOD and is presented in a UItableView. Is there a way to make some content invulnerable. For example, can I make "Kiwi"

undeletable.

Something like, let i = fruit.index(where: "Kiwi") let IArr = [0...fruit.count] IArr = IArr.filter{$0 != i}

// removes Kiwi index

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()}

      

However, this does not work because it indexPath

cannot accept array types. How can i do this?

+3


source to share


2 answers


You can check that the string in the pointer path is not 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 */
    }
}

      

+3


source


unless you show some of the editing when you slide to the left. You can use this.



 func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {

     let FRUIT = fruit[indexPath.row]

     if (FRUIT != "Kiwi") {
        return true
     }

     return false

    }

      

0


source







All Articles