【问题标题】:UICollectionView not populating from Array with dataUICollectionView 没有从数组中填充数据
【发布时间】:2019-09-09 21:41:47
【问题描述】:

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

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

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

         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?


    }

【问题讨论】:

    标签: swift uicollectionview


    【解决方案1】:

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

    【讨论】:

    • 我已经添加了该代码,并且 IBOoutlet 已连接,但仍然没有结果。它从不调用 Cell 函数 byte 打印出 firebase 结果。可能是这行代码 collectionView?.register(DJProfileCell.self, forCellWithReuseIdentifier: profileCellID)
    • setup() 是否在单元类中被调用?在单元格内,您最好将内部视图添加到“.contentView”属性。我建议您下载并尝试使用示例集合视图项目,因为您的代码包含几个问题。在自己的 xib 中设计单元格,确保集合具有正确的约束等。
    • 我在应用程序中已经有几个 uicollection 视图,但由于某种原因这不起作用。唯一的区别是它使用故事板而不是程序化。 cell 函数永远不会被调用,因此 setup 函数也不会被调用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-30
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    相关资源
    最近更新 更多