Ios 细胞图像显示不正常,为什么会发生这种情况?
我正在构建一个图像提要,该提要在表视图中包含一个后跟用户名标签的图像。由于某些原因,单元格图像与正确的用户名不匹配。看起来,图像的下载顺序与用户名不同,但事实并非如此。请帮忙 编辑:在完成一些控制台日志记录之后,我看到图像被下载并附加到图像数组中,顺序与用户名不同。但我不知道为什么,也不知道如何修复它Ios 细胞图像显示不正常,为什么会发生这种情况?,ios,xcode,swift,parse-platform,Ios,Xcode,Swift,Parse Platform,我正在构建一个图像提要,该提要在表视图中包含一个后跟用户名标签的图像。由于某些原因,单元格图像与正确的用户名不匹配。看起来,图像的下载顺序与用户名不同,但事实并非如此。请帮忙 编辑:在完成一些控制台日志记录之后,我看到图像被下载并附加到图像数组中,顺序与用户名不同。但我不知道为什么,也不知道如何修复它 import UIKit class TrendingChallengeTableViewCell: UITableViewCell { @IBOutlet var postImag
import UIKit
class TrendingChallengeTableViewCell: UITableViewCell {
@IBOutlet var postImage: UIImageView!
@IBOutlet var postComment: UILabel!
override func prepareForReuse() {
super.prepareForReuse()
self.postImage.image = nil
self.postComment.text = ""
}
}
import UIKit
import Parse
var pressedChallenge: String?
class TrendingChallengeTableViewController: UITableViewController {
var images = [PFFile]()
var usernames = [String]()
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.hidesBackButton = true
let query = PFQuery(className: "Post")
query.whereKey("imageComment", equalTo: pressedChallenge!)
query.findObjectsInBackgroundWithBlock { (object, error) -> Void in
self.usernames.removeAll(keepCapacity: true)
self.images.removeAll(keepCapacity: true)
if let object = object {
for images in object {
self.images.append(images["imageFile"] as! PFFile)
let userQuery = PFUser.query()
userQuery?.whereKey("_id", equalTo: images["userId"])
userQuery?.findObjectsInBackgroundWithBlock({ (object, error) -> Void in
if let object = object {
for user in object {
self.usernames.append(user["username"] as! String)
self.tableView.reloadData()
}
}
})
}
}
}
}
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 usernames.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let trendCell = tableView.dequeueReusableCellWithIdentifier("trendingCell", forIndexPath: indexPath) as! TrendingChallengeTableViewCell
trendCell.postComment.text = "\(usernames[indexPath.row]) completed the \(pressedChallenge!) challenge!"
images[indexPath.row].getDataInBackgroundWithBlock { (data, error) -> Void in
if let downloadedImage = UIImage(data: data!) {
trendCell.postImage.image = downloadedImage
}
}
return trendCell
}
}
在获取对象之前,尝试告诉两个查询以特定方式排序,即:
let query = PFQuery(className: "Post")
query.whereKey("imageComment", equalTo: pressedChallenge!)
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock { (object, error) -> Void in
//etc
}
对第二个查询也要这样做。不确定这将如何工作。第一个查询查找图像文件,第二个查询在发布图像的单独表中查找用户名。我不知道用户查询会发生什么样的排序,因为没有createdAt字段。你试过了吗?我非常确定默认情况下所有解析对象都有一个createdAt字段。是的,这是正确的,但是,当创建位置的用户与用户发布图像的时间无关时。我只是想看看,结果很糟糕。你还能想到别的吗?