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

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

我有一个列出电影的 PFQueryTableViewController。在我的导航栏中,我有一个过滤器按钮,当用户按下它时,它会显示一个 UIPopoverPresentationController。这个弹出窗口只是在 UITableView 中显示一些选项。见下图:

目标

当用户在弹出窗口中选择一个选项时,我需要将选项索引传回主PFQueryTableViewController,然后相应地更新table。并关闭弹出控制器。

我已经知道如何对 table 进行排序,我只需要知道如何将所选选项传回,然后如何将其添加到 if 语句中以过滤我的 Parse询问。例如,如果用户选择按最高评分过滤,在我的 queryForTable() 函数中,我会输入类似:

if XXXXXXXXXXXX {

query.orderByDescending("highestRated")

}

而且我已经创建了弹出窗口 VC 并且可以正常工作。

希望这是有道理的...如果没有,请询​​问更多信息。我的弹出窗口 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
}

}

您可以在此处使用协议/委托。您的 popviewcontroller 将是 tableviewcontroller 或将具有 table 视图。您必须将此控制器称为弹出窗口。现在在 popOverController 中 didselect 调用委托函数并根据您的要求将选项 select 作为 string/Index 传递。此委托方法将在您的 viewcontroller 中实现,您可以在其中排序和重新加载 table.

这是我创建的演示。您可以根据您的要求进行更新。

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

在 ViewController.swift 中检查函数

func sortSelection(selectedItem: Int)

这是委托函数,当您在 Popover 中 select 时将被调用。如果需要,您也可以传递字符串而不是 Int。