细胞图像显示无序,为什么会发生这种情况?

时间:2016-02-22 18:13:16

标签: ios xcode swift parse-platform

我构建了一个图像Feed,其中包含一个图像,后面跟着一个表格视图中的用户名标签。出于某种原因,单元格图像与正确的用户名不匹配。几乎看起来图像的下载顺序与用户名不同,但情况可能并非如此。请帮忙!

编辑:执行一些控制台日志记录后,我看到图像被下载并附加到图像数组,其顺序与用户名不同。但我不知道为什么,也不知道如何解决它。

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
    }


}

1 个答案:

答案 0 :(得分:0)

在获取对象之前尝试告诉您的查询以特定方式排序,即:

let query = PFQuery(className: "Post")
query.whereKey("imageComment", equalTo: pressedChallenge!)
query.orderByDescending("createdAt")
query.findObjectsInBackgroundWithBlock { (object, error) -> Void in
    //etc
}

同样为第二个查询执行此操作。