mercredi 24 juin 2015

How can I populate one UITableView with two queries?

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)        
}

See more ..

Aucun commentaire:

Enregistrer un commentaire