2016-05-17 1 views
1

Je fais une vue de table dans Swift dont chaque cellule contient un bouton. Lorsqu'un bouton est appuyé, un survol s'affiche sur ce bouton.swift: Popover sur le bouton dans la cellule de la table affichage toujours sur la première cellule

Mon problème est, peu importe la cellule sur laquelle je clique sur le bouton à l'intérieur, popover toujours afficher sur la première cellule.

Veuillez voir les images jointes pour plus de compréhension.

table popover

Ci-dessous est mon code en classe tableviewcontroller. J'utilise tag pour détecter le toucher sur le bouton.

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     // Table view cells are reused and should be dequeued using a cell identifier. 
     let cellIdentifier = "Cell01" 
     let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as? UICell01 
     //when tap share button 
     cell!.shareButton.tag = indexPath.row 
     cell!.shareButton.addTarget(self, action: "shareAction:", forControlEvents: .TouchUpInside)  
     return cell! 
    } 






    @IBAction func shareAction(sender: UIButton) { 
     //Create Action Sheet to be menu 
     let alert:UIAlertController = UIAlertController(title: "Share on", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet) 
     let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) 
     { 
      UIAlertAction in 
     } 
     // Add the actions 
     alert.addAction(cancelAction) 

     // Present the controller 
     if UIDevice.currentDevice().userInterfaceIdiom == .Phone 
     { 
      self.presentViewController(alert, animated: true, completion: nil) 
     } 
     else //iPad 
     { 
      var popover:UIPopoverController?=nil 
      popover = UIPopoverController(contentViewController: alert) 
      popover!.presentPopoverFromRect(sender.frame, inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true) 
     } 

Je pense que le problème est à la dernière ligne. "expéditeur" qui est passé à la fonction est le bouton dans la première cellule seulement. Comment puis-je résoudre le problème?

Répondre

0

Vous référencez sender.frame par rapport à sa propre cellule et n'incorporez pas le cadre de la cellule par rapport au UITableView. Vérifiez le superview de l'expéditeur.

0

Essayez de déclarer shareAction dans une classe client UITableViewCell et assignez-la à la cellule. Comme ci-dessous

import UIKit 

class CellCustome: UITableViewCell { 


    override func awakeFromNib() { 
     super.awakeFromNib() 
     // Initialization code 
    } 

    @IBAction func shareAction(sender: UIButton) { 
     //Create Action Sheet to be menu 
     let alert:UIAlertController = UIAlertController(title: "Share on", message: nil, preferredStyle: UIAlertControllerStyle.ActionSheet) 
     let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) 
     { 
      UIAlertAction in 
     } 
     // Add the actions 
     alert.addAction(cancelAction) 

     // Present the controller 
     if UIDevice.currentDevice().userInterfaceIdiom == .Phone 
     { 
      self.presentViewController(alert, animated: true, completion: nil) 
     } 
     else //iPad 
     { 
      var popover:UIPopoverController?=nil 
      popover = UIPopoverController(contentViewController: alert) 
      popover!.presentPopoverFromRect(sender.frame, inView: self.view, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true) 
     } 

    } 

    override func setSelected(selected: Bool, animated: Bool) { 
     super.setSelected(selected, animated: animated) 

     // Configure the view for the selected state 
    } 

} 
0

Merci à tous pour m'éclairer.

je peux simplement résoudre en changeant la dernière ligne

popover!.presentPopoverFromRect(sender.frame, inView: sender.superview!, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true) 

Maintenant, le ballon popover affiche sur le bouton correct je touche.

Mais je ne comprends toujours pas l'argument inView.

Si c'est self.view, le ballon popover s'affiche sur la première cellule. si est sender.superview, le ballon Popover s'affiche sur la cellule correcte.