Search code examples
iosswiftuitableviewswift3

popover controller with tableview in swift


I have a TableView with a bar button item. When I click bar button it's show the PopOver menu. when I click the Popover menu items perform some actions (like tableview data with specific items) I want use PopOver menu as a Filter type:

This is the code :

View Controller code

@IBAction func ButtonClick(_ sender: UIBarButtonItem) {
     self.performSegue(withIdentifier: "POP1", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier ==  "POP1"            
    {
        let dest = segue.destination

        if let pop =  dest.popoverPresentationController
        {                
            pop.delegate = self
        }
    }
}

func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
    return .none
}

 func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return fruits.count
}

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    let fruitName = fruits[indexPath.row]
    cell.textLabel?.text = fruitName

    return cell
}

PopOVER Code:

override func numberOfSections(in tableView: UITableView) -> Int {

    return 1

}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
           return data.count

}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "POP", for: indexPath)

    cell.textLabel?.text = data[indexPath.row]
    return cell
}


override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    var selectedItem = indexPath
    print (selectedItem.row)

}

enter image description here

when I click one in Popover menu it should show the count of words starting with each letter (1 from A, 2 from B) displayed on view controller table view.


Solution

  • You can use either a completion closure or delegate method to achieve your goal. This is a useful lib using completion closure with generic data type for you to freely get/set table view data source as key-value tuples.

    For delegation, you can do something as follows:

    protocol PopoverViewControllerDelegate: class {
        func didSelectData(_ result: String)
    }
    
    class PopoverViewController: UIViewController {
        weak var delegate: PopoverViewControllerDelegate?
    
        //...
    
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            dismiss(animated: true, completion: nil)
            let selectedItem = arrayData[indexPath.row]
            delegate?.didSelectData(selectedItem)
        }
    }
    

    and then in your current view controller:

    class ViewController: UIViewController, PopoverViewControllerDelegate {
    
        // ...
    
        func didSelectData(_ result: String) {
            // Update fruits array
            tableView.reload()
        }
    }