2015-03-27 18 views
11

Próbuję usunąć wiersz w widoku tabeli. Zaimplementowałem wymagane metody, ale kiedy przesuniemy wiersz w poziomie, nie ma przycisku usuwania. Szukałem i znalazłem to samo rozwiązanie wszędzie, ale nie działa w moim przypadku. Nie wiem, gdzie popełniam błąd. Czy ktoś może mi pomóc?usunąć wiersz w widoku tabeli w swift

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool 
{ 
    return true 
} 

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 
{ 
    if editingStyle == .Delete 
    { 
     dataHandler.deletePeripheral(indexPath.row) 
     tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) 
    } 
} 
+0

myślę, że u sprawdzić składnie funkcji –

+0

funkcjono tableView (tableView: UITableView, wyd commitEditingStyle itingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { –

+0

@LeonardoSavioDabus Użyłem tej samej składni, co sugerowałeś i zaktualizowałem również moje pytanie, ale przycisk usuwania nie nadchodzi. –

Odpowiedz

38

Jeśli poniżej kodowanie jest pomocne dla Ciebie, będę szczęśliwy

func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool 
{ 
    return true 
} 

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 
{ 
    if editingStyle == .Delete 
    { 
     yourArray.removeAtIndex(indexPath.row) 
     self.tableView.reloadData() 
    } 
} 

SWIFT 3,0

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

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) 
{ 
    if editingStyle == .delete 
    { 
     yourArray.remove(at: indexPath.row) 
     tblDltRow.reloadData() 
    } 
} 

Musisz odświeżyć stół.

+11

tableView.deleteRowsAtIndexPaths ([indexPath], withRowAnimation: .Fade) to lepszy pomysł niż ponowne załadowanie całej tabeli –

2
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 
{ 
if editingStyle == UITableViewCellEditingStyle.Delete { 
    numbers.removeAtIndex(indexPath.row)  
    tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 
} 
} 
+0

Używam widoku tabeli, a nie kontrolerów widoku tabeli, nie mogę użyć przesłonięcia i mam zaimplementowaną metodę w ten sam sposób, ale to nie działa. –

7
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 

     if (editingStyle == UITableViewCellEditingStyle.Delete) { 


      self.tableView.beginUpdates() 
      self.arrayData.removeObjectAtIndex(indexPath.row) // also remove an array object if exists. 
      self.tableView.deleteRowsAtIndexPaths(NSArray(object: NSIndexPath(forRow: indexPath.row, inSection: 2)), withRowAnimation: UITableViewRowAnimation.Left) 
      self.tableView.endUpdates() 

     } 
+2

Metody beginUpdate() i endUpdate() rozwiązały mój problem, w którym aplikacja ulega awarii, gdy po prostu próbuję wywołać DeleteRow w Swift 3 –

0

wiem co masz prob. Myślę, że po prostu sprawdzić swoje ograniczenia tabeli mogą być źle po prostu ponownie sprawdzić twoje ograniczenia auto układ

3

Zastosuj wiązania z wiązaniem automatycznym do widoku tabeli. Przycisk kasowania jest tam, ale nie są wyświetlane z powodu braku układ automatycznego

+0

Miałem (niektóre) ograniczenia, ale najwyraźniej za mało. Autolayout to sztuka, a nie nauka.:) – mobibob

0
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
       if editingStyle == UITableViewCellEditingStyle.Delete { 
    yourArray.removeAtIndex(indexPath.row) 

    tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 

    } 

} 
0
func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool 
{ 
return true 
} 

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) 
{ 
if editingStyle == .Delete 
{ 
    carsArray.removeAtIndex(indexPath.row) 
    self.tableView.reloadData() 
} 
} 
1

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 :)

+0

commit edittingStyle działa dla mnie w Swift 3 .. napisałem działający przykład na ich .. twoja metoda działała również dla mnie. wydaje się lepiej. – nyxee

1

dla Swift 3

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if editingStyle == UITableViewCellEditingStyle.delete { 
     dataHandler.deletePeripheral(indexPath.row) 
     tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) 
     // - OR - 
     // mTableView.reloadData() //mTableView is an outlet for the tableView 
    } 
} 
Powiązane problemy