[英]How can I populate one UITableView with two queries?
我有一个 tableview,需要显示用户参与的所有好友请求(发送和接收)。
但是,我能想到的唯一方法是执行两个单独的查询:一个是用户名等于请求者,另一个是用户名等于请求者。
这是最好的路线吗? 如果是这样,我该如何设置代码?
我的代码在let dateString3 = timePeriodFormatter.stringFromDate(pendingDates[indexPath.row])
上的func cellForRowAtIndexPath()
上的应用程序不断崩溃,说数组超出范围。
这是我到目前为止:
@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)
}
我完全忘记了语法是什么,因为我使用 parse 已经有一段时间了,但是我在我构建的应用程序中具有类似的功能。 Parse 允许您分别创建两个查询,然后在调用之前将它们连接在一起,以便返回两个单独查询的结果。
由于您尝试运行的查询在同一个类中,您可以执行以下操作:
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])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.