简体   繁体   中英

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

want to add a UICollectionView inside UITableView by downloading data from firebase to show Now I'm done with the interface part, but I'm stuck in the problem, can't bring data from firebase to show in the UICollectionView .

I can't run collectionView.reloadData() because the UICollectionView is different in class, how should I fix it?

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)
    }
}

In class that extends UICollectionView , put one setter method like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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