栃木県のJavaエンジニア、WEBプログラマーのためのサイト

2015.11.11[Swift] UITableView セルを選択したらPopoverを表示

UIAlertController

  • iPadを想定
  • UIAlertControllerのスタイルをActionSheet
  • popoverPresentationControllerプロパティにソースを設定

"Popover"

    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .ActionSheet)
        alertController.popoverPresentationController?.sourceView = view
        alertController.popoverPresentationController?.sourceRect = tableView.cellForRowAtIndexPath(indexPath)!.frame
        alertController.addAction(UIAlertAction(title: "", style: .Cancel, handler: { _ in
            self.tableView.deselectRowAtIndexPath(indexPath, animated: false)
            print("cancel")
        }))
        alertController.addAction(UIAlertAction(title: "Item1", style: .Default, handler: { _ in
            self.tableView.deselectRowAtIndexPath(indexPath, animated: false)
            print("select item1")
        }))
        alertController.addAction(UIAlertAction(title: "Item2", style: .Default, handler: { _ in
            self.tableView.deselectRowAtIndexPath(indexPath, animated: false)
            print("select item2")
        }))
        alertController.addAction(UIAlertAction(title: "Item3", style: .Default, handler: { _ in
            self.tableView.deselectRowAtIndexPath(indexPath, animated: false)
            print("select item3")
        }))
        
        presentViewController(alertController, animated: false, completion: nil)
    }