2017-12-23 10 views
0

Nicht sicher, warum, aber die obere Zeile meines uitableview kann nicht bearbeitet werden, alle anderen Zeilen funktionieren wie normal und löschen wie erwartet. Es ist wie caneditrowat indexPath: Indexpath funktioniert nicht für diese eine Zeile. Siehe Bilder im Anhang.Die oberste Zeile in uitableview kann nicht bearbeitet/gelöscht werden - Swift

Mein Code in tableView (_: commit: forRowAt :) sieht aus wie alle Tutorials, die ich finden kann, scheint keine anderen Beispiele mit diesem Problem zu finden.

//MARK: Properties 
    var favouriteExercises = [FavouriteExercise]() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    //Load exercises from local DB 
    if let savedFavouriteExercises = loadFavouriteExercises() 
    { 
     //loading exercises in from the favourites 
     favouriteExercises += savedFavouriteExercises 
    } 

    // Use the edit button item provided by the table view controller. 
    navigationItem.rightBarButtonItem = editButtonItem 
} 

override func numberOfSections(in tableView: UITableView) -> Int { 
    return 1 
} 

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    //count number of rows in table 
    return favouriteExercises.count 
} 

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    // Table view cells are reused and should be dequeued using a cell identifier. 
    let cellIdentifier = "FavouriteTableViewCell" 

    guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? FavouriteTableViewCell else { 
     fatalError("The dequeued cell is not an instance of FavouriteTableViewCell.") 
    } 

    // Fetches the appropriate exercise for the data source layout. 
    let exercise = favouriteExercises[indexPath.row] 

    //setup the layout for the cell in the table view 
    cell.nameLabel.text = exercise.name 
    let url = URL(string: (exercise.iconUrl))! 
    cell.photoImageView.sd_setImage(with: url) 
    //cell.photoImageView.image = #imageLiteral(resourceName: "defaultPhoto") 
    cell.backgroundColor = UIColor.darkGray 

    return cell 
} 

// Override to support conditional editing of the table view. 
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { 
    // Return false if you do not want the specified item to be editable. 
    return true 
} 

// Override to support editing the table view. 
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if editingStyle == .delete { 
     // Delete the row from the data source 
     favouriteExercises.remove(at: indexPath.row) 
     saveFavouriteExercisess() 
     tableView.deleteRows(at: [indexPath], with: .fade) 
    } 
} 

Danke für die Hilfe

+1

Der einzig mögliche Weg könnte sein => func Tableview (_ Tableview: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {if indexPath.row == 0 {return UITableViewCellEditingStyle.none} gibt UITableViewCellEditingStyle.delete} zurück. –

+0

Vielen Dank @KumarReddy! Benutzte deine Lösung ohne die if-Anweisung und es funktionierte. –

+0

Machen Sie meinen Kommentar als hilfreich :) und bis abstimmen –

Antwort

1
override func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle 
    { 
     return UITableViewCellEditingStyle.delete 
    } 

Antwort von @ Lösung KumarReddy die angepasst

Verwandte Themen