swift - UICollectionView 未从数组中填充数据

标签 swift uicollectionview

我有一个包含 115 个对象的数组,其中包含来自 Firebase 的名称和照片网址字符串。打印数据显示结果,这样我就知道它正确地提取数据。

问题是单元格永远不会被数据填充。

如果我在 DJProfileCell: UICollectionViewCell 类中添加打印(名称),它永远不会被调用,所以我相信这就是问题所在。

         class VLCDJProfileViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout {

            @IBOutlet weak var similarArtistsCollection: UICollectionView!

            var ref: DatabaseReference!
            let profileCellID = "cellId"
            var djObject = SimilarDJ(image: "image1", name: "name1")
            var djsSimilarArray = [SimilarDJ]()


        override func viewDidLoad() {
                super.viewDidLoad()
                ref = Database.database().reference()
                loadDJs()

                collectionView?.register(DJProfileCell.self, forCellWithReuseIdentifier: profileCellID)

            }

        func loadDJs(){

                let allDJs = self.ref.child("DJ")

                allDJs.observeSingleEvent(of: .value, with: { snapshot in
                    for child in snapshot.children {
                        let snap = child as! DataSnapshot
                        let djsDict = snap.value as! [String: Any]
                        let PhotoUrl = djsDict["PhotoUrl"] as! String
                        let name = djsDict["name"] as! String + "    "


                        self.djObject = SimilarDJ   (image: PhotoUrl, name: name + "   ")
                        self.djsSimilarArray.append(self.djObject)


                        self.similarArtistsCollection.reloadData();


                    }
                })
            }

        override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return djsSimilarArray.count
        }

        override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: profileCellID, for: indexPath) as! DJProfileCell

            cell.djprofile = djsSimilarArray[indexPath.item]

            return cell
        }

    class DJProfileCell: UICollectionViewCell {


        var djprofile: SimilarDJ?  {
            didSet {

                guard let djImageProfile = djprofile?.image else {return}
                guard let djNameProfile = djprofile?.name else {return}

                let url = URL(string: djImageProfile)
                djImageView.kf.indicatorType = .activity
                djImageView.kf.setImage(with: url)
                djImageLabel.text = djNameProfile
                djImageLabel.adjustsFontSizeToFitWidth = true


            }
        }

        override init(frame: CGRect) {
            super.init(frame: frame)
setup()

        }

func setup(){


    self.backgroundColor = .white
    self.addSubview(djImageView)
    self.addSubview(djImageLabel)

    }



        let djImageView: UIImageView = {

            let iv = UIImageView()
        //    iv.contentMode = .scaleAspectFit
        //    iv.backgroundColor = .green
            return iv

        }()

        let djImageLabel: MarqueeLabel = {

            let label = MarqueeLabel()
            label.text = "Name"
            label.textColor = UIColor.black
            label.font = label.font.withSize(14)

            label.textAlignment = .center

            return label

        }()



        required init?(coder aDecoder: NSCoder) {
            fatalError("init has not been implemented")
        }

    }

    struct SimilarDJ {

        let image: String?
        let name: String?


    }

最佳答案

在单元格类中 - djImageView 和 djImageLabel 永远不会添加到 View 的层次结构中。我没有看到 IBOutlet 也没有 addSubview()。

关于swift - UICollectionView 未从数组中填充数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57861550/

相关文章:

javascript - 在 UIWebView swift 中单击按钮时打开 ViewController

swift - handleWatchKitExtentionRequest ...如何访问回复元素以将值返回给 watchkit 扩展?

swift - 如何在 swift 4 中正确使用 sqlite3 数据库中的追加函数

swift - UICollectionView 无法正确滚动

ios - Collectionview 固定单元格间距

ios - UICollectionView 的 viewDidLoad 大小不正确

iOS 14 应用内购买请求登录/验证两次

ios - UIViewController 部分标题未显示

ios - 点击时从 UICollectionView 中的单元格获取图像

ios - 如何突出显示选定的 UICollectionView 单元格? ( swift )