简体   繁体   English

传回Tableview单元格数据以在Swift中的PFQueryTableViewController中过滤对象

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

I have a PFQueryTableViewController which lists films. 我有一个PFQueryTableViewController列出了电影。 In my navigation bar, I have a filter button, which when the user presses it, it displays a UIPopoverPresentationController . 在我的导航栏中,我有一个过滤器按钮,当用户按下该按钮时,它会显示一个UIPopoverPresentationController This popover simply displays some options in a UITableView. 此弹出窗口仅在UITableView中显示一些选项。 See the image below: 见下图:

在此处输入图片说明

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. 当用户在弹出窗口中选择一个选项时,我需要将选项索引传递回主PFQueryTableViewController ,然后相应地更新表。 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. 我已经知道如何对表进行排序,我只需要知道如何将选定的选项传递回去,然后如何将其添加到if语句中以过滤我的Parse查询。 For example if the user selected to filter by Highest rated, in my queryForTable() function, I'll put something like: 例如,如果用户选择按最高评分进行过滤,则在我的queryForTable()函数中,我将输入:

if XXXXXXXXXXXX {

query.orderByDescending("highestRated")

}

And I've already created the popover VC and it works. 而且我已经创建了popover VC,它可以工作。

Hopefully this makes sense...if not please ask more info. 希望这很有道理...如果没有,请询​​问更多信息。 The code for my popover VC is below: 我的弹出式VC的代码如下:

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
}

}

You can use protocol / Delegates here. 您可以在此处使用协议/代理。 Your popviewcontroller will be a tableviewcontroller or will have a tableview. 您的popviewcontroller将是tableviewcontroller或将具有tableview。 You have to call this controller as popover. 您必须将此控制器称为popover。 Now in popOverController on didselect call the delegate function and pass the option select as an string/Index as per your requirement. 现在,在didSelect上的popOverController中,调用委托函数,并根据您的要求将选项select作为字符串/索引传递。 This delegate method will be implementated in your viewcontroller, where you can sort and reload the table. 该委托方法将在您的viewcontroller中实现,您可以在其中对表进行排序和重新加载。

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 https://github.com/quantumarun/Demos/tree/master/PopOverDemo

In ViewController.swift check for function 在ViewController.swift中检查功能

func sortSelection(selectedItem: Int)

This is the delegate function which will be called when you select in Popover. 这是在Popover中选择时将调用的委托函数。 If you require you can pass string as well instead of Int. 如果需要,您也可以传递字符串而不是Int。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM