2016-05-17 9 views
1

各セルにボタンが含まれているSwiftでテーブルビューを作成しています。 ボタンをタップすると、そのボタンにポップオーバーが表示されます。swift:テーブルビューのセル上のポップオーバーボタンは常に最初のセルに表示されます

私の問題は、どのセルをボタンをクリックしても、最初のセルに常にポップオーバーが表示されることです。

詳しくは添付の画像をご覧ください。以下は

table popover

tableviewcontrollerクラスで私のコードです。私はボタンをタッチすることを検出するためにタグを使用します。

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

問題は最終行にあると思います。機能に渡される「送信者」は、最初のセルのボタンだけです。どのように問題を解決するのですか?

答えて

0

sender.frameを参照しており、UITableViewを基準にしたセルのフレームを組み込んでいません。送信者のsuperviewを確認してください。

0

カスタマーのUITableViewCellクラス内でshareActionを宣言し、それをセルにアサートしてみます。以下のように

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

ありがとうございました。

私は単に今

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

に私は触れ正しいボタンのポップオーバーのバルーンが表示され、最後の行を変更することで解決することができます。

しかし、私はまだinView引数を理解していません。

self.viewの場合、最初のセルにポップオーバーバルーンが表示されます。 isがsender.superviewの場合、ポップオーバーのバルーンが正しいセルに表示されます。

関連する問題