how to display popover in TableView cell in swift

Follow this steps and get popOver
STEP 1:- Create New Project "popOverDemo"
STEP 2:- install pod "Popover"
STEP 3:-  and Drag and drop UITableView.
STEP 4:- set The consternt.
STEP 5:- Copy and Pest this code.

import UIKit
import Popover

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UIPopoverPresentationControllerDelegate {
    // MARK:- ---------- OUTLET Declaration ------------
    @IBOutlet var myTableViewObj: UITableView!

    // MARK:- ---------- Variable Declaration ------------
    var userNameArray = [String]()
    fileprivate var popover: Popover!
    

   //MARK:- ------- View Life Cycle Functions ---------
    override func viewDidLoad() {
        super.viewDidLoad()
        myTableViewObj.delegate = self
        myTableViewObj.dataSource = self
        userNameArray = ["Robart","Tom","Jon","Piter","Harry","Robart","Tom","Jon"]
       
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    //MARK:- ---- TableView Delegate and DataSource Functions ----
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return userNameArray.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = myTableViewObj.dequeueReusableCell(withIdentifier: "MyTableViewCell") as! MyTableViewCell
       
        cell.userNamelbl.text = userNameArray[indexPath.row]
        cell.ClickHereButton.tag = indexPath.row
        cell.ClickHereButton.addTarget(self, action: #selector(ClickHereButtonAction(_:)), for: .touchUpInside)
  
        return cell
    }
    

    override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
        self.popover.dismiss()
    }
   
    func popoverPresentationControllerDidDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) {
        self.popover.dismiss()
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
       
        self.popover.dismiss()
        super.touchesBegan(touches, with: event)
    }


    //MARK:- ------- Objective Functions ---------

   

    @objc func ClickHereButtonAction(_ sender:UIButton)
    {
        let cell = myTableViewObj.cellForRow(at:IndexPath(row: sender.tag, section: 0) as IndexPath) as! MyTableViewCell
        let aView = UIView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width/2, height: 30))
        // Button one
        let btn1 = UIButton()
        btn1.frame = CGRect(x: 1, y: UIScreen.main.bounds.minY + 15, width: UIScreen.main.bounds.size.width/5, height: 20)
        btn1.setTitle("Button1", for: .normal)
        btn1.setTitleColor(UIColor.black, for: .normal)
        btn1.addTarget(self, action: #selector(ButtonOneAction), for: .touchUpInside)
       
        // Button two
        let btn2 = UIButton()
        btn2.frame = CGRect(x: btn1.bounds.maxX, y: UIScreen.main.bounds.minY + 15, width: UIScreen.main.bounds.size.width/5, height: 20)
        btn2.setTitle("Button2", for: .normal)
        btn2.setTitleColor(UIColor.black, for: .normal)
        btn2.addTarget(self, action: #selector(ButtonTwoAction), for: .touchUpInside)
       
        //Add Buttons in Dinamic View
        aView.addSubview(btn1)
        aView.addSubview(btn2)
       
        // popover view
        let popover = Popover(options: nil, showHandler: nil, dismissHandler: nil)
        popover.blackOverlayColor = UIColor.clear
        let minY = Int(self.view.frame.minY + cell.frame.midY )
        popover.show(aView, point: CGPoint(x: 300, y: minY), inView: myTableViewObj)
    }

    @objc func ButtonOneAction(_ sender:UIButton)
    {
        print("this is Button One")
    }
    @objc func ButtonTwoAction(_ sender:UIButton)
    {
        print("this is Button Two")
    }
   
}
// MARK:- --------- my CollectionView cell class ------------
class MyTableViewCell: UITableViewCell {
    @IBOutlet var ClickHereButton:UIButton!
    @IBOutlet var ButtonOne:UIButton!
    @IBOutlet var ButtonTwo:UIButton!
    @IBOutlet var userNamelbl:UILabel!
    @IBOutlet var ButtonsView: UIButton!

}

I Hop you Enjoy this tutorial keep touch in this blog for more update 

Comments

Popular posts from this blog

Windows Keys

Important extensions while create a new App in swift