2017-05-02 1 views
2

Je crée une vue de table et je souhaite créer une fonction dans laquelle vous pouvez supprimer une ligne en la balayant vers la droite et en appuyant sur le bouton de suppression. Moi et mon professeur ont essayé pendant environ une demi-heure pour résoudre ce problème mais rien ne semble fonctionner.UITableView Supprimer la ligne

import UIKit 

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    var StoredValues = Values() 
    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 
    override func didReceiveMemoryWarning() {// 
     super.didReceiveMemoryWarning() 
    } 

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     return UITableViewCell() 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 1 
    } 
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     self.performSegue(withIdentifier: "meunSegue", sender: self) 

     func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
      _ = segue.destination as! SecondViewController 
     } 
    } 
    func tableView->(UITableView *)tableView canEditRowsAtIndexPath:(NSIndexPath *)indexPath { 
    return YES 
    } 
    - (void)tableView:(UITableView *)tableView committEditStyle: (UITableViewCellEditingStyle) 
    func tableView { 
     var cell = UITableView.self 
     var Animation = UITableViewRowAnimation(rawValue: 1) 
     if editingStyle == UITableViewCellEditingStyle.delete { 
      cell.deleteRows(indexPath) 
      //cell.deleteRows(at: [NSIndexPath], with: UITableViewRowAnimation.automatic) 

     } 
    } 


    class SecondViewController: UIViewController { 

     var recievedData = "" 
     override func viewDidLoad() { 
      super.viewDidLoad() 
      print(recievedData) 
     } 
    } 
} 
+0

Votre code source est un mélange impair de Swift et d'Objective-C. Vous pouvez nettoyer votre exemple de code afin qu'il compile et édite votre question avec un meilleur code. –

Répondre

0

C'est parce que votre implémentation de source de données numberOfRowsInSection retourne toujours 1 (fixe).

Généralement, vous stockez des objets dans un tableau, qui définit le nombre de lignes. Et, commitEditingStyle doit supprimer l'objet de la matrice, puis supprimez la ligne.

– (void)tableView: (UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath: (NSIndexPath *)indexPath {if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [maTheData removeObjectAtIndex:[indexPath row]]; 
     // Delete row using the cool literal version of [NSArray arrayWithObject:indexPath] 

     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 

    } 
} 

Suivez ce link pour plus de détails.

0

Cette fonction rapide permet de supprimer une ligne en la balayant vers la droite et en appuyant sur le bouton Supprimer. En fait, cette fonction supprime l'élément du tableau des éléments, puis supprime la ligne. En outre, cette ligne a été supprimée de tableView.

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if editingStyle == UITableViewCellEditingStyle.delete { 
     // your items include cell variables 
     items.remove(at: indexPath.row) 
     tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) 
    } 
}