找到你要的答案

Q:How can I populate one UITableView with two queries?

Q:我怎么能用两个查询填充一个表格吗?

I have a tableview that needs to display all friend requests that a user is involved in (both sent and received.)

However, the only way I can think of to do this is to perform two separate queries: one where the username is equal to the requester, and one where the username is equal to the requested.

Is this the best route to go? If so, how do I set up the code?

My code keeps crashing the app on func cellForRowAtIndexPath() on let dateString3 = timePeriodFormatter.stringFromDate(pendingDates[indexPath.row]) saying that the array is out of range.

This is what I have so far:

@IBOutlet weak var tableView: UITableView!

override func viewWillAppear(animated: Bool) {

    UIApplication.sharedApplication().beginIgnoringInteractionEvents()

    users = []
    user = PFUser()
    objectId = String()
    pendingDates = []
    username = String()
    usernames = []
    tradeIdentifier = String()
    tradeId = []
    pendingRequestsId = []

    var requestsQuery = PFQuery(className: "Requests")
    requestsQuery.whereKey("status", equalTo: "transit")
    requestsQuery.whereKey("completed", notEqualTo: PFUser.currentUser()!.username!)
    requestsQuery.whereKey("completed", notEqualTo: "complete")
    requestsQuery.whereKey("requestedUsername", equalTo: PFUser.currentUser()!.username!)
    requestsQuery.orderByDescending("createdAt")
    requestsQuery.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in
        if error == nil {
            if let objects = objects as? [PFObject] {
                for object in objects {
                    pendingDates.append(object.createdAt! as NSDate)
                    usernames.append(object["requesterUsername"] as! String)
                    users.append(object["requester"] as! PFUser)
                    pendingRequestsId.append(object.objectId!)
                    tradeId.append("requested")
                }
            }
            var requestsQuery2 = PFQuery(className: "Requests")
            requestsQuery2.whereKey("status", equalTo: "transit")
            requestsQuery2.whereKey("completed", notEqualTo: PFUser.currentUser()!.username!)
            requestsQuery2.whereKey("completed", notEqualTo: "complete")
            requestsQuery2.whereKey("requesterUsername", equalTo: PFUser.currentUser()!.username!)
            requestsQuery2.orderByDescending("createdAt")
            requestsQuery2.findObjectsInBackgroundWithBlock {
                (objects: [AnyObject]?, error: NSError?) -> Void in
                if error == nil {
                    if let objects = objects as? [PFObject] {
                        for object in objects {
                            pendingDates.append(object.createdAt! as NSDate)
                            usernames.append(object["requestedUsername"] as! String)
                            users.append(object["requested"] as! PFUser)
                            pendingRequestsId.append(object.objectId!)
                            tradeId.append("requester")

                        }
                    }
                    UIApplication.sharedApplication().endIgnoringInteractionEvents()
                } else {
                    UIApplication.sharedApplication().endIgnoringInteractionEvents()
                    println(error)
                }
                //self.tableView.reloadData()
            }
        } else {
            println(error)
        }
        self.tableView.reloadData()
    }


}

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

    return usernames.count

}

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

    let cell: PendingTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! PendingTableViewCell

    cell.username.text = usernames[indexPath.row] as String

    let timePeriodFormatter = NSDateFormatter()
    timePeriodFormatter.dateFormat = "EEE, MMM d"

    let dateString3 = timePeriodFormatter.stringFromDate(pendingDates[indexPath.row])

    cell.date.text = dateString3

    return cell

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    username = usernames[indexPath.row]
    objectId = pendingRequestsId[indexPath.row] as String
    tradeIdentifier = tradeId[indexPath.row]
    user = users[indexPath.row]
    self.performSegueWithIdentifier("showDetails", sender: self)        
}

我有一个TableView,需要显示一个用户参与所有朋友的请求(发送和接收的。)

然而,我能想到的唯一的办法就是执行两个单独的查询:一个用户名为请求者,和一个用户名等于要求。

这是最好的路线吗?如果是的话,我如何设置代码?

我的代码总是死机的应用程序在功能上cellforrowatindexpath()让datestring3 = timeperiodformatter。stringfromdate(pendingdates [ indexpath。行])说,数组越界。

这就是我迄今所拥有的:

@IBOutlet weak var tableView: UITableView!

override func viewWillAppear(animated: Bool) {

    UIApplication.sharedApplication().beginIgnoringInteractionEvents()

    users = []
    user = PFUser()
    objectId = String()
    pendingDates = []
    username = String()
    usernames = []
    tradeIdentifier = String()
    tradeId = []
    pendingRequestsId = []

    var requestsQuery = PFQuery(className: "Requests")
    requestsQuery.whereKey("status", equalTo: "transit")
    requestsQuery.whereKey("completed", notEqualTo: PFUser.currentUser()!.username!)
    requestsQuery.whereKey("completed", notEqualTo: "complete")
    requestsQuery.whereKey("requestedUsername", equalTo: PFUser.currentUser()!.username!)
    requestsQuery.orderByDescending("createdAt")
    requestsQuery.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in
        if error == nil {
            if let objects = objects as? [PFObject] {
                for object in objects {
                    pendingDates.append(object.createdAt! as NSDate)
                    usernames.append(object["requesterUsername"] as! String)
                    users.append(object["requester"] as! PFUser)
                    pendingRequestsId.append(object.objectId!)
                    tradeId.append("requested")
                }
            }
            var requestsQuery2 = PFQuery(className: "Requests")
            requestsQuery2.whereKey("status", equalTo: "transit")
            requestsQuery2.whereKey("completed", notEqualTo: PFUser.currentUser()!.username!)
            requestsQuery2.whereKey("completed", notEqualTo: "complete")
            requestsQuery2.whereKey("requesterUsername", equalTo: PFUser.currentUser()!.username!)
            requestsQuery2.orderByDescending("createdAt")
            requestsQuery2.findObjectsInBackgroundWithBlock {
                (objects: [AnyObject]?, error: NSError?) -> Void in
                if error == nil {
                    if let objects = objects as? [PFObject] {
                        for object in objects {
                            pendingDates.append(object.createdAt! as NSDate)
                            usernames.append(object["requestedUsername"] as! String)
                            users.append(object["requested"] as! PFUser)
                            pendingRequestsId.append(object.objectId!)
                            tradeId.append("requester")

                        }
                    }
                    UIApplication.sharedApplication().endIgnoringInteractionEvents()
                } else {
                    UIApplication.sharedApplication().endIgnoringInteractionEvents()
                    println(error)
                }
                //self.tableView.reloadData()
            }
        } else {
            println(error)
        }
        self.tableView.reloadData()
    }


}

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

    return usernames.count

}

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

    let cell: PendingTableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! PendingTableViewCell

    cell.username.text = usernames[indexPath.row] as String

    let timePeriodFormatter = NSDateFormatter()
    timePeriodFormatter.dateFormat = "EEE, MMM d"

    let dateString3 = timePeriodFormatter.stringFromDate(pendingDates[indexPath.row])

    cell.date.text = dateString3

    return cell

}

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    username = usernames[indexPath.row]
    objectId = pendingRequestsId[indexPath.row] as String
    tradeIdentifier = tradeId[indexPath.row]
    user = users[indexPath.row]
    self.performSegueWithIdentifier("showDetails", sender: self)        
}
answer1: 回答1:

I forget exactly what the syntax is because it's been awhile since I've worked with parse, but I had similar funtionality in an app that I built. Parse lets you create two queries seperately and then join them together before the call is made so as to return the result of two individual queries.

Since the queries you're trying to run are in the same class you can do something like this:

var requestQuery1 = PFQuery(className: "Requests")
requestQuery1.whereKey("status", equalTo: "Transit")
//add other query paremeter here
var requestQuery2 = PFQuery(className: "Requests)
//add query paremeters

And then here is where you would run the two queries together:

var joinQuery = PFQuery.orQueryWithSubqueries([requestQuery1, requestQuery2])

我忘了确切的语法是因为它是因为我曾与解析已一段时间,但我也有类似的功能在一个应用程序,我建。分析可以让你创建两个查询,然后分别加入他们一起在进行调用之前以回报两个人查询的结果。

既然你试图运行的查询都在同一个类,你可以这样做:

var requestQuery1 = PFQuery(className: "Requests")
requestQuery1.whereKey("status", equalTo: "Transit")
//add other query paremeter here
var requestQuery2 = PFQuery(className: "Requests)
//add query paremeters

然后在这里你将运行两个查询一起:

var joinQuery = PFQuery.orQueryWithSubqueries([requestQuery1, requestQuery2])
ios  swift  uitableview  parse.com