【问题标题】:UICollectionView Display oldest post first, i want it to display latest postUICollectionView 首先显示最旧的帖子,我希望它显示最新的帖子
【发布时间】:2018-05-05 18:40:17
【问题描述】:
var posts = [Post]()
....
func collectionView(_ collectionView: UICollectionView, cellForItemAt   indexPath: IndexPath) -> UICollectionViewCell{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as! FeedCollectionViewCell
cell.postTextView.text = posts[indexPath.item].caption
cell.postImageView.image = UIImage(named: "photoplaceholder.jpg")
let urlString = posts[indexPath.item].photoUrl
let url = URL(string:  urlString)
let task = URLSession.shared.dataTask(with: url!){ data, reponse, error     in
    if error != nil {
        print(error!)
    } else{
        DispatchQueue.main.async(execute: {
            cell.postImageView.image = UIImage(data: data!)
        })

    }

}
 task.resume()
return cell

}
}

我想显示最新的帖子而不是最旧的帖子。 而对于 tableview,posts[indexPath.row].caption 给了我最新的帖子, 对于 UICollectionView 它没有,它给了我最古老的帖子。我需要有关如何在其他帖子之前先显示最新帖子的帮助。

【问题讨论】:

    标签: ios swift firebase uicollectionview


    【解决方案1】:

    UITableViewUICollectionView 之间的数据顺序应该没有任何差异,因此您可能应该调查为什么它会发生,而不是如何在 cellForItemAt 中修复它.

    但是要回答如何解决它;因为我看到你只有一个部分,你可以简单地做 posts.reversed()[indexPath.row].captionposts.reversed()[indexPath.row].photoUrl

    【讨论】:

    • 很好,这解决了我面临的问题:posts.reversed()[indexPath.row].photoUrl
    猜你喜欢
    • 2012-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    相关资源
    最近更新 更多