【问题标题】:Cell images are showing out of order, why is this happening?单元格图像显示无序,为什么会发生这种情况?
【发布时间】:2016-02-22 18:13:15
【问题描述】:

我正在构建一个图像提要,其中包括一个图像,后跟一个表格视图内的用户名标签。出于某种原因,单元格图像与正确的用户名不匹配。似乎图像的下载顺序与用户名不同,但事实并非如此。请帮忙!

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

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
    }


}

【问题讨论】:

    标签: ios xcode swift parse-platform


    【解决方案1】:

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

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

    对第二个查询也这样做。

    【讨论】:

    • 不确定这将如何工作。第一个查询查找图像文件,第二个查询在发布图像的单独表中查找用户名。我没有看到用户查询会发生什么样的排序,因为没有 createdAt 字段。
    • 你试过了吗?我很确定所有 Parse 对象默认都有一个 createdAt 字段。
    • 是的,这是正确的,但是,当创建的用户与用户发布图像的时间无关时。我只是试了一下,结果很糟糕。你还能想到什么?
    猜你喜欢
    • 2010-09-22
    • 2021-01-24
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 2018-04-27
    • 2013-08-02
    • 2018-12-19
    • 1970-01-01
    相关资源
    最近更新 更多