繁体   English   中英

想通过从firebase下载数据来显示在tableView中添加一个collectionView。 ios App Swift

[英]want to add a collectionView inside tableView by downloading data from firebase to show . ios App Swift

想通过从UICollectionView下载数据来在UITableView添加一个UICollectionView来显示现在我已经完成了接口部分,但是我UICollectionView了问题,无法将数据从UICollectionView显示。

我无法运行collectionView.reloadData()因为UICollectionView在类中是不同的,我该如何修复它?

func showImageRewardData(rewardID:String) {
        let databaseRef = Database.database().reference().child("reward").child(rewardID).child("rewardImage")
        databaseRef.observe(DataEventType.value, with: { (Snapshot) in
            if Snapshot.childrenCount>0{
                self.rewardDataArr.removeAll()
                for rewardImage in Snapshot.children.allObjects as! [DataSnapshot]{
                    let rewardObject = rewardImage.value as? [String: AnyObject]

                    if(rewardObject?["imageURL"] != nil){
                        let imageUrl = rewardObject?["imageURL"]
                        let Data = rewardDetailClass(rewardImage: imageUrl as? String)
                        self.rewardDataArr.insert(Data, at: 0)          
                    }

YOURVIEWCONTROLLER.SWIFT

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let CellIdentifier: String = "Cell_NotesList"
    var cell: Cell_NotesList? = (tableView.dequeueReusableCell(withIdentifier: CellIdentifier) as? Cell_NotesList)

    if cell == nil {
        let topLevelObjects: [Any] = Bundle.main.loadNibNamed("Cell_NotesList", owner: nil, options: nil)!
        cell = (topLevelObjects[0] as? Cell_NotesList)
        cell?.selectionStyle = .none
    }
    cell?.reloadCollectionView(arr: arrofofthumbImages)
    return cell!
}

YourCell.SWIFT

class Cell_NotesList: UITableViewCell {

    var imagesArr = NSMutableArray()

    override func awakeFromNib() {
        collectionView.register(UINib.init(nibName: "cell_ImageCollection", bundle: nil), forCellWithReuseIdentifier: "cell_ImageCollection")
        super.awakeFromNib()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }
}



extension Cell_NotesList : UICollectionViewDataSource {

    func reloadCollectionView(arr:NSMutableArray) {
        imagesArr = arr
        collectionView.reloadData()
        self.layoutIfNeeded()
    }

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell : cell_ImageCollection? = collectionView.dequeueReusableCell(withReuseIdentifier: "cell_ImageCollection", for: indexPath) as? cell_ImageCollection
        //YOUR CODE HERE
        return cell!
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    }
}

extension Cell_NotesList : UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 120.0, height: 120.0)
    }
}

extension UICollectionViewCell {
    var indexPath: IndexPath? {
        return (superview as? UICollectionView)?.indexPath(for: self)
    }
}

在扩展UICollectionView类中,放置一个setter方法

func setData(data: String){
    // Update your cell view here like
    Label.text = data
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM