Search code examples
iosswiftuitableviewnsindexpathpfquerytableviewcontrolle

Pass back Tableview Cell Data to filter objects in my PFQueryTableViewController in Swift


I have a PFQueryTableViewController which lists films. In my navigation bar, I have a filter button, which when the user presses it, it displays a UIPopoverPresentationController. This popover simply displays some options in a UITableView. See the image below:

enter image description here

Goal

When the user selects an option in the popover, I need the option index to be passed back to the main PFQueryTableViewController and then update the table accordingly. And to also close the popover controller.

I already know how I can sort the table, I just need to know how to pass the selected option back, and then how to add it into the if statement to filter my Parse query. For example if the user selected to filter by Highest rated, in my queryForTable() function, I'll put something like:

if XXXXXXXXXXXX {

query.orderByDescending("highestRated")

}

And I've already created the popover VC and it works.

Hopefully this makes sense...if not please ask more info. The code for my popover VC is below:

class SortByViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var sortByTableView: UITableView!

var sortByOptions = ["Date added", "Film name", "Our star rating", "Highest rated", "Lowest rated", "Director's name"]

override func viewDidLoad() {
    super.viewDidLoad()

    self.sortByTableView.rowHeight = 44.0

    sortByTableView.tableFooterView = UIView(frame:CGRectZero)

    sortByTableView.backgroundColor = UIColor.clearColor()


}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return sortByOptions.count

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = sortByTableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel?.text = self.sortByOptions[indexPath.row]

    let imageName = UIImage(named: sortByOptions[indexPath.row])
    cell.imageView?.image = imageName

    let itemSize:CGSize = CGSizeMake(30, 30)
    UIGraphicsBeginImageContextWithOptions(itemSize, false, UIScreen.mainScreen().scale)
    let imageRect : CGRect = CGRectMake(0, 0, itemSize.width, itemSize.height)
    cell.imageView!.image?.drawInRect(imageRect)
    cell.imageView!.image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    cell.textLabel?.textColor = UIColor(hue: 359/360, saturation: 67/100, brightness: 71/100, alpha: 1)

    cell.backgroundColor = UIColor.clearColor()

    return cell
}

}

Solution

  • You can use protocol / Delegates here. Your popviewcontroller will be a tableviewcontroller or will have a tableview. You have to call this controller as popover. Now in popOverController on didselect call the delegate function and pass the option select as an string/Index as per your requirement. This delegate method will be implementated in your viewcontroller, where you can sort and reload the table.

    Here is the demo which i have created. You can update the same as per your requirement.

    https://github.com/quantumarun/Demos/tree/master/PopOverDemo

    In ViewController.swift check for function

    func sortSelection(selectedItem: Int)
    

    This is the delegate function which will be called when you select in Popover. If you require you can pass string as well instead of Int.