UITableviewCell中的collectionView numberOfItemsInSection

时间:2019-06-18 10:59:38

标签: ios swift uitableview uicollectionview

我在collectionView单元内制作了一个tableView

我注册了所有代表和协议,并且在大多数情况下一切正常

问题在于collectionView必须根据服务器响应动态生成单元格

我在集合的顶部安装了一个计数器,该计数器显示有多少个单元格(也可以动态更新)

如果在加载屏幕时集合位于视野中,则将加载单元格

如果必须完成收集,则它为空,尽管计数器会指示元素的存在。

我不太明白为什么。下面的代码

代码形式VC:

 let realPfotoCell = tableView.dequeueReusableCell(withIdentifier: 
"RealPhoto", for: indexPath)
            as! RealPhotoCarInfoTableViewCell
        realPfotoCell.delegate = self


        realPfotoCell.model = self.rPhoto
        if(self.rPhoto?.data != nil){
            realPfotoCell.RPhotoCount.text = String(self.rPhoto.data!.count)
        }



        cell = realPfotoCell
        cell.selectionStyle = UITableViewCell.SelectionStyle.none
    }

来自tableViewCell的代码:

    var model: RealPhoto!

    func setCollectionViewDataSourceDelegate(dataSourceDelegate: UICollectionViewDataSource & UICollectionViewDelegate, forRow row: Int) {
        MyCollection.delegate = self
        MyCollection.dataSource = self
        MyCollection.reloadData()

    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
MyCollection.reloadData()
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    }



    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        if (self.model?.data?.count != nil){
            print("Photos here")
            print(self.model?.data!.count as Any)
        } else {
            print("No photos")
        }

        return self.model?.data?.count ?? 0

    }

2 个答案:

答案 0 :(得分:0)

请确保在服务器响应到来时调用ReloadData()。

   //Server Response
    MyCollection.delegate = self
    MyCollection.dataSource = self
    MyCollection.reloadData()

答案 1 :(得分:0)

已解决

在表格单元格上的第一个创建此功能:

func collectionReloadData(){
        DispatchQueue.main.async(execute: {
            self.collectionView.reloadData()
        })
    }

然后从

调用

func tableView(_ tableView:UITableView,cellForRowAt indexPath:IndexPath)-> UITableViewCell {...

realPfotoCell.collectionReloadData()
相关问题