2015-06-23 16 views
8

I wdrożone editActionsForRowAtIndexPath i commitEditingStyle bezstykowa działa, ale ma akcji edit pojawiają się na UITableViewCellI wdrożone editActionsForRowAtIndexPath i commitEditingStyle ale ma akcji edit pojawiają się na tableViewCell kiedy przesuwając komórkę

moja realizacja dla editActionsForRowAtIndexPath i commitEditingStyle następujące:

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { 
     if editingStyle == UITableViewCellEditingStyle.Delete { 
      //I did some work here 
      tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic) 
     } 
    } 


func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? { 

    let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.Normal, title: "Delete" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in 
     //I did some work here 
     tableView.reloadData() 
    }) 


    return [deleteAction] 
} 

Każda pomoc będzie mile widziane

+1

zrobić ci impliemtn canEditRowAtIndexPath wrócić YES –

+0

tak zrobiłem i działa w innym viewController bez wdrożenia canEditRowAtIndexPath – Abdelrahman

Odpowiedz

2

trzeba wdrożyć canEditRowAtIndexPath from the UITableview Delegate Methods i return true.

+0

Mam już zaimplementować canEditRowAtIndexPath, ale nadal nie działa – Abdelrahman

+0

problemem jest to, że editActions pojawiają się w innym ViewController zi bez implementacji canEditRowAtIndexPath, ale w innych ViewController nie pojawiają się, ale machnięcie działa dobrze – Abdelrahman

+0

Czy drugi kontroler jest podklasą UIViewController lub podklasy z UITableView Kontroler lub inna klasa, w której dziedziczysz funkcję? –

9

Myślę, że wymieszałeś tutaj dwa różne rodzaje edycji.

Pierwszy rodzaj edycji to stary UITableViewCellEditingStyle.Delete. A nowym sposobem jest zapewnienie niestandardowego widoku akcesoriów.

Jeśli zaimplementujesz niestandardowy widok akcesoriów, domyślne przyciski usuwania nie będą wyświetlane, a zatem nie będą wywoływane. Tak więc, z mojego punktu widzenia, nie można nawet wywołać.

dokumentacji Apple Dla editActionsForRowAtIndexPath zawiera następujące sentense: If you do not implement this method, the table view displays the standard accessory buttons when the user swipes the row. Sądziłem, że jeśli wdrożenie tej metody, średnia widok akcesoria nie będą widoczne.

Edit: przykładem Code (zaktualizowana Swift 3 11/17/16)

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

private func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: IndexPath) -> [AnyObject]? { 
    let deleteAction = UITableViewRowAction(style: UITableViewRowActionStyle.normal, title: "Delete" , handler: { (action:UITableViewRowAction, indexPath:IndexPath) -> Void in 
    }) 
    return [deleteAction] 
} 

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 

} 

Edit 2: Jak rajagp zwraca uwagę, jeśli nie trzeba pusty realizację jeżeli celujesz tylko w system iOS9 (lub nowszy).

+0

I wdrożone editActionsForRowAtIndexPath ale widok akcesorium nie jest pokazany – Abdelrahman

+0

gdybym nie wdrażają func Tableview (Tableview: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) Przesuwanie nie działa – Abdelrahman

+1

Potrzebujesz pustej implementacji, aby uruchomić. – gyan

0

Właśnie skopiowałem ViewController, gdzie przyciski akcji wiersza są wyświetlane i działają dobrze i zastępują ten, w którym przyciski akcji wiersza nie pojawiają się podczas przesuwania, a teraz przyciski akcji wiersza są wyświetlane i oczekiwane zachowanie.

Ale nie rozumiem problemu. czy ktoś może to wyjaśnić?

1

Spróbuj dodać pewne działania wewnątrz przycisk Usuń

func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]? 
    { 
     let delete = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Delete" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in 
      let alertView = UIAlertController(title: "Delete Action", message: "", preferredStyle: UIAlertControllerStyle.Alert) 
      alertView.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)) 
      UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertView, animated: true, completion: nil) 

     }) 
     delete.backgroundColor = UIColor.redColor() 


     let more = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "More" , handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in 
      let alertView = UIAlertController(title: "More Action", message: "", preferredStyle: UIAlertControllerStyle.Alert) 
      alertView.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)) 
      UIApplication.sharedApplication().keyWindow?.rootViewController?.presentViewController(alertView, animated: true, completion: nil) 
     }) 
     more.backgroundColor = UIColor.blueColor() 

     return [delete, more] 
    } 
1

TLDR:

niezbędne funkcje do wdrożenia: • commitEditingStyle • canEditRowAt • editActionsForRowAt

Uwaga: dla editActionsForRowAt nie można powrócić pusta tablica - zepsuje wszystkie komórki. Jeśli istnieje określony typ wiersza, dla którego nie chcesz zezwolić na działania edycyjne, określ to w canEditRowAt, aby zwrócić wartość false dla tego typu komórki.

0

Niestety do wstawania taką starą wątek, ale mam to działa na Swift 3 wykonawcze:

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle { 
    return .delete 
} 
Powiązane problemy