簡體   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