2017-01-20 5 views
0

Actuellement, j'ai un CustomTableViewCell qui est utilisé dans quatre ou cinq endroits différents. La cellule personnalisée a une propriété UILongPressGestureRecognizer chargée paresseuse qui est ajoutée en tant que reconnaissance de mouvement dans cellForRowAtIndexPath dans le conteneur virtuel parent.Motifs d'affichage d'un pop-up Toast avec Custom UITableViewCell

self.tableView.addGestureRecognizer(cell.longPress) 

Lorsqu'un utilisateur lance la longue presse, je veux une notification de toast à afficher des informations popup de contexte, puis disparaître au bout de quelques secondes. J'ai inclus cela dans mon code pour le CustomTableViewCell, mais toutes ces décisions commencent à "sentir". Y a-t-il une façon plus intelligente et plus logique de mettre en œuvre ces décisions?

Cette cellule de tableau d'affichage a le code suivant:

weak var parentTableView: UITableView? 

lazy var longPress: UILongPressGestureRecognizer = { 

    let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPressSelector)) 

    longPress.minimumPressDuration = 0.5 
    longPress.delegate = self 

    return longPress 

}() 

func longPressSelector(_ longPress: UILongPressGestureRecognizer!) { 

    if let tableView = self.parentTableView { 

     let point = longPress.location(in: tableView) 

     let indexPath = tableView.indexPathForRow(at: point) 

     if ((indexPath! as NSIndexPath).section == 0 && longPress.state == .began) { 


      // do some work 

      // Show informational popup 
      let toast = createToastNotification(withTitle: addedSong.name) 

      Timer.scheduledTimer(withTimeInterval: 2.0, repeats: false) { (timer) -> Void in 
       UIView.animate(withDuration: 1.0) {() -> Void in 
        toast.alpha = 0.0 
        toast = nil 
       } 
      } 
     } 
    } 

} 

func createToastNotification(withTitle title: String) -> UIView { 

    if let tableView = self.parentTableView { 
     let windowFrame = tableView.superview?.bounds 

     let height:CGFloat = 145, width: CGFloat = 145 

     let x = (windowFrame?.width)!/2 - width/2 
     let y = (windowFrame?.height)!/2 - height/2 

     let toast = EnsembleToastView.create() 

     toast.frame = CGRect(x: x, y: y, width: width, height: height) 
     toast.songLabel.text = title 
     toast.layer.cornerRadius = 5 

     tableView.superview?.addSubview(toast) 

     return toast 
    } 

    return UIView() 
} 

Répondre

1

Je pense qu'il est plus logique pour le TableView de savoir comment afficher un toast si je créerais un protocole dans votre tableViewCell, donc je prendrais les étapes suivantes.

  • Faire le TableViewController responsable:
    • créer des toasts (une seule fois)
    • configuration du pain grillé
    • montrant du pain grillé
    • répondre au geste de presse à long
    • configuration table cellule de vue
  • Laisser YourTableViewCell seulement déléguer

Alors, faisons répondre à temps presse geste premier

protocol TableViewCellLongPressDelegate { 
    func tableViewCellHadLongPress(_ cell: YourTableViewCell) 
} 

étendons ensuite votre TableViewController pour se conformer à votre nouveau protocole

extension YourTableViewController : TableViewCellLongPressDelegate { 
    func tableViewCellHadLongPress(_ cell: YourTableViewCell){ 
     //configure toast based on which cell long pressed 
     configureToastNotification(with title: cell.title){ 
     //show toast 
    } 
} 

Maintenant, configuration vue table de cellules au sein de votre TableViewController configurer votre cellule et affecter le TableViewController comme longPressDelegate

let cell = YourTableViewCell.dequeue(from: self.tableView)! 
//configure cell 
cell.tableViewCellLongPressDelegate = self 

Cette approche est agréable parce que vous pouvez déplacer la méthode createToastNotification() à votre TableViewController et responsable de la création des toasts (une seule fois)

var toastNotification : UIView? 
viewDidLoad(){ 
    //yatta yatta 
    toastNotification = createToastNotification() 
} 

Ensuite, vous pouvez changer créer ToastNotification à

func createToastNotification() -> UIView? { 

    let windowFrame = self.bounds 

    let height:CGFloat = 145, width: CGFloat = 145 

    let x = (windowFrame?.width)!/2 - width/2 
    let y = (windowFrame?.height)!/2 - height/2 

    let toast = EnsembleToastView.create() 

    toast.frame = CGRect(x: x, y: y, width: width, height: height) 
    toast.layer.cornerRadius = 5 

    self.addSubview(toast) 

    return toast 
} 

Enfin pour YourTableViewController, configuration du pain grillé, nous allons créer un configureToastNotification (avec le titre: String) comme:

func configureToastNotification(with title: String){ 
    if let toast = self.toastNotification { 
     toast.songLabel.text = title 
    } 
} 

Pour la fin, nous supprimer un beaucoup de la responsabilité de YourTableViewCell et lui permettent de déléguer seulement:

protocol TableViewCellLongPressDelegate : class { 
    func tableViewCellHadLongPress(_ cell: YourTableViewCell) 
} 

class YourTableViewCell : UITableViewCell { 

    //initializers 

    weak var longPressDelegate: TableViewCellLongPressDelegate? 

    lazy var longPress: UILongPressGestureRecognizer = { 

     let longPress = UILongPressGestureRecognizer(target: self, action: #selector(longPressHappened)) 

     longPress.minimumPressDuration = 0.5 
     longPress.delegate = self 

     return longPress 

    }() 

    func longPressHappened() { 
     self.longPressDelegate?.tableViewCellHadLongPress(self) 
    } 
} 
+0

C'était parfait. J'avais déjà commencé à travailler dans la direction de délégué, et votre édition qui a inclus le nouveau sélecteur sur le CustomTableViewCell l'a mise par dessus. Merci de votre aide! – dstepan

+0

@dstepan Content de pouvoir aider. –