如何用数据数组填充 UICollectionView?

How to populate UICollectionView with array of data?

我有一个 UICollectionView,我用来自 AWS Amplify List Query 函数的数据填充了它。该函数的数据数组成功打印到控制台,但它只有 returns 1 个单元格,其中包含一个 authuserPost 中的元素和相应的字段。我已将结构 UserPostData 设置为等于 posts 数组中的元素。我的问题是我的 for 循环只获取一个 post 的数据,并且在应该有 5 个单元格时只显示一个单元格。然而,当我 print(element) 它显示所有 5 post 与他们自己的元素。谁能指出我做错了什么的正确方向?

这是我的代码:

import UIKit
import Amplify
import AmplifyPlugins
import AWSMobileClient

struct UserPostData {
    var id: String?
    var userSub: String?
    var zipcode: String?
    var contactMethod: String?
    
}

class UserPostsViewController: UIViewController {
    
    var userSubID = String()
    var numberOfPosts = Int(0)
    var data = [UserPostData]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        fetchData()
        setupUI()
    }
    
    lazy var collectionView: UICollectionView = {
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: collectionViewLayout())
        collectionView.register(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell")
        collectionView.backgroundColor = .tertiarySystemBackground
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        return collectionView
    }()
    
    func fetchData(){
        userSubID = AWSMobileClient.default().userSub!
        let post = authuserPost.keys
        let predicate = post.userSub == userSubID
        _ = Amplify.API.query(request: .list(authuserPost.self, where: predicate)) { event in
            switch event {
            case .success(let result):
                switch result {
                case .success(let posts):
                    DispatchQueue.main.async {
                        self.numberOfPosts = posts.count
                        self.collectionView.reloadData()
                        
                        for element in posts{
                            self.data = [UserPostData.init(id: element.id, userSub: element.userSub, zipcode: element.zipcode, contactMethod: element.contactMethod)]
                            print(element) //When I print element it shows all post and their elements. The Output will be at the bottom of the question.
                            
                        }
                        
                        //print("Successfully retrieved list of posts: \(posts)")
                    }
                case .failure(let error):
                    print("Got failed result with \(error.errorDescription)")
                }
            case .failure(let error):
                print("Got failed event with error \(error)")
            }
        }
        
    }
    
}

// MARK: - UICollectionViewDelegate & Data Source
extension UserPostsViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data.count // It only returns one cell
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
        cell.dogImageView.image = UIImage(named: "image1")
        cell.zipcodeLabel.text = data[indexPath.section].zipcode
        cell.contactMethod.text = data[indexPath.section].contactMethod
        cell.layoutSubviews()
        return cell
    }
    
}

当我在 for 循环中 print(element) 时,我将其发送到控制台。

authuserPost(id: "0600D60E-2270-4B1A-AE8C-7E42F2B32F9C", userSub: "47bcd9ba-78f9-4d2f-9685-8a97b3a69e77", zipcode: "30316", contactMethod: "(444)085-5666")
authuserPost(id: "6FDD8A2D-D6A6-4CDC-AD05-5D5E085178DD", userSub: "47bcd9ba-78f9-4d2f-9685-8a97b3a69e77",  zipcode: "30308", contactMethod: "(444)869-6665")
authuserPost(id: "58A3431C-7AAE-4AB0-B89E-87D932C09017", userSub: "47bcd9ba-78f9-4d2f-9685-8a97b3a69e77", zipcode: "30316", contactMethod: "(444)869-4534")
authuserPost(id: "C9DF31BE-6FC0-410C-90EE-24E13617199F", userSub: "47bcd9ba-78f9-4d2f-9685-8a97b3a69e77", zipcode: "30316", contactMethod: "(444)869-4534")
authuserPost(id: "5BD76C91-7D3A-424B-99F7-6330EE0AD6AF", userSub: "47bcd9ba-78f9-4d2f-9685-8a97b3a69e77", zipcode: "30316", contactMethod: "(444)869-3453")

当您遍历 posts 数组并将 [UserPostData.init(...)] 分配给它时,您实际上是在重新分配 self.data 属性又一遍。只有最后一个能活下来。

因此,您应该在迭代中为每个 post 追加 UserPostData,而不是分配给数组。


你应该更换

self.data = [UserPostData.init(id: element.id, userSub: element.userSub, zipcode: element.zipcode, contactMethod: element.contactMethod)]

self.data.append(UserPostData.init(id: element.id, userSub: element.userSub, zipcode: element.zipcode, contactMethod: element.contactMethod))