Walczyłem z tym, jak dobrze, ale w końcu okazało się rozwiązanie - Używam XCode 8.3.2. Wiele odpowiedzi, które przeczytałem, dotyczyło starszych wersji XCode/Swift, a wiele z nich dotyczyło Objective-C.
Rozwiązaniem, które znalazłem, było użycie narzędzia "editActionsForRowAt" dla UITableViews. Uwaga: Wszystkie inne elementy, które czytałem, wskazywały na narzędzie "commit dla UITableView" , ale nigdy nie mogłem go uruchomić. Używanie funkcji editActionsForRowAt działa doskonale dla mnie.
UWAGA: "postData" to nazwa tablicy, do której dodałem dane.
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let deleteAction = UITableViewRowAction(style: .default, title: "Delete", handler: { (action, IndexPath) in
// Remove item from the array
postData.remove(at: IndexPath.row)
// Delete the row from the table view
tableView.deleteRows(at: [IndexPath as IndexPath], with: .fade)
})
// set DeleteAction Button to RED (this line is optional - it simply allows you to change the color of the Delete button - remove it if you wish)
deleteAction.backgroundColor = UIColor.red
return [deleteAction]
}
Mam też trochę kodu w celu dodania 'EDIT' przycisk obok przycisku Usuń:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let editAction = UITableViewRowAction(style: .default, title: "Edit", handler: { (action, IndexPath) in
//print("Edit tapped")
})
// Set EditAction button to blue (this line is not mandatory - it just sets the color of the Edit box to blue)
editAction.backgroundColor = UIColor.blue
let deleteAction = UITableViewRowAction(style: .default, title: "Delete", handler: { (action, IndexPath) in
//print("Delete tapped")
// Remove item from the array
postData.remove(at: IndexPath.row)
// Delete the row from the table view
tableView.deleteRows(at: [IndexPath as IndexPath], with: .fade)
})
// set DeleteAction Button to RED (this line isn't mandatory - it just sets the color of the Delete box)
deleteAction.backgroundColor = UIColor.green
return [editAction, deleteAction]
}
EDIT: stały format kodu tak pokazała się w szarym polu :)
myślę, że u sprawdzić składnie funkcji –
funkcjono tableView (tableView: UITableView, wyd commitEditingStyle itingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { –
@LeonardoSavioDabus Użyłem tej samej składni, co sugerowałeś i zaktualizowałem również moje pytanie, ale przycisk usuwania nie nadchodzi. –