gpt4 book ai didi

swift - 查询中的查询不附加 Swift/Parse

转载 作者:行者123 更新时间:2023-11-28 06:37:26 24 4
gpt4 key购买 nike

我希望有人能提供帮助,因为这可能是一个简单的问题,但我在查询中的 Parse 查询打印了我正在寻找的对象,但无法将它附加到我需要它的数组中以在 UITableView 中检索它。

当我将单元格中的查询结果出列时,错误是“ fatal error :索引超出范围”。

代码如下:

导入 UIKit导入解析进口 bolt

类 MessagesTableVC: UITableViewController {

var usernames = [String]()
var sentDate = [NSDate]()
var details = [String]()
var userImage = [PFFile]()


@IBAction func backToProfile(sender: AnyObject) {
self.performSegueWithIdentifier("messagesToProfile", sender: self)
}


override func viewDidLoad() {
super.viewDidLoad()

let messagesQuery = PFQuery(className: "Messages")
messagesQuery.whereKey("recipientId", equalTo: PFUser.currentUser()!.objectId!)
messagesQuery.includeKey("senderId")
messagesQuery.orderByDescending("createdAt")
messagesQuery.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

if error != nil {
print(error)
}

if error == nil {

if let objects = objects {

self.usernames.removeAll(keepCapacity: true)
self.sentDate.removeAll(keepCapacity: true)
self.details.removeAll(keepCapacity: true)
self.userImage.removeAll(keepCapacity: true)

for object in objects {

self.sentDate.append(object.createdAt! as NSDate)

if (object["item"] != nil) {
self.details.append(object["item"] as! String)
} else {
self.details.append(object["request"] as! String)
}

let senderObject = (object["senderId"] as! PFUser)
let senderId = (senderObject.objectId! as String)

print(senderId)

// Query for sender info
let userQuery = PFUser.query()
userQuery?.whereKey("objectId", equalTo: senderId)
userQuery?.getFirstObjectInBackgroundWithBlock({ (object, error) in

self.usernames.append((object!["username"] as! String))

//self.userImage.append(object!["profilePicture"] as! PFFile)

})




}

dispatch_async(dispatch_get_main_queue()) {
self.tableView.reloadData()
//self.search.resignFirstResponder()
}
}


}
})

// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false

// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem()
}


override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}


override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return sentDate.count
}


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("messageCell", forIndexPath: indexPath) as! MessageTableViewCell

//print(usernames[indexPath.row])
cell.senderUsername.text = usernames[indexPath.row]
cell.itemOrPreview.text = details[indexPath.row]

let date = sentDate[indexPath.row]
let formatter = NSDateFormatter()
formatter.dateStyle = NSDateFormatterStyle.LongStyle
formatter.timeStyle = .ShortStyle
let dateString = formatter.stringFromDate(date)
cell.sentDate.text = dateString


//userImage[indexPath.row].getDataInBackgroundWithBlock { (data, error) in
// if let downloadedItemImage = UIImage(data: data!) {
// cell.senderImage?.image = downloadedItemImage
//}
//}


return cell
}

override func viewWillAppear(animated: Bool) {
tableView.reloadData()
}

最佳答案

正如我猜测的那样,您可能会在 sentDateusernames 中得到不一致的元素数量,因为您通过异步 附加到 usernames getFirstObjectInBackgroundWithBlock 方法,因此,当您在 tableView 上调用 reloadData 时,所有用户名可能尚未添加到 usernames。当您的 tableView 回调触发时,usernames 中的项目可能比 sentDate 中的项目少,而在 numberOfItems 中您返回 sentDate 中的项目数。

为了修复您需要首先重构您的代码,它有很多地方可能会出错。我不会给出任何具体建议,但看起来,您可能希望在重新加载 tableView 之前等待所有数据。

关于swift - 查询中的查询不附加 Swift/Parse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38816068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com