这是我尝试从 namesArray
中的用户的解析中获取图像的函数。
func fetchData(){
let imagePredicate = NSPredicate(format: "username IN %@", namesArray)
let imageQuery = PFQuery(className: "_User", predicate: imagePredicate)
imageQuery.findObjectsInBackgroundWithBlock { (objects: [PFObject]?, error: NSError?) -> Void in
if error == nil {
for object in objects! {
self.imagesArray.append(object["image"] as! PFFile)
if self.imagesArray.count == self.namesArray.count {
self.tableView.reloadData()
}
} else {
print("error: \(error?.localizedDescription)")
}
}
}
这是我的 tableView:cellForRowAtIndexPath
方法:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell") as! ChatsCell
cell.nameLabel.text = namesArray[indexPath.row]
if imagesArray.count == namesArray.count && self.imagesLoaded == false{
imagesArray[indexPath.row].getDataInBackgroundWithBlock { (imageData: NSData?, error: NSError?) -> Void in
if error == nil {
let image = UIImage(data: imageData!)
cell.imageView?.image = image
self.tableView.reloadData()
self.imagesLoaded = true
}
}
}
return cell
}
但是当我这样做时,我发现图像与用户名称不同步。即使我将用户按其他顺序排列,图像也将保持与之前相同的顺序。 我该如何更改它?
最佳答案
不知道你在这里问什么。您是否期望图像以用户排序的数组形式返回?
如果是这样,那么您将需要向 PFQuery 添加排序顺序。我建议您按用户名对 namesArray 进行排序,然后也按用户名对 imageQuery 进行排序:
imageQuery.orderByDescending("username")
希望我理解这个问题;]
--T
关于ios - 使用谓词从数组中获取无序数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34806954/