【问题标题】:collectionView numberOfItemsInSection in UITableviewCellUITableviewCell 中的 collectionView numberOfItemsInSection
【发布时间】:2019-06-18 10:59:38
【问题描述】:

我在tableView 单元格中创建了一个collectionView

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

问题在于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

    }

【问题讨论】:

  • 在收到服务器响应后,您是否重新加载了collectionview
  • 你的意思是collectionView.reloaddata()?
  • collectionView.reloaddata()
  • 我在awakeFromNib () 函数中执行此操作生成单元后,我没有重新启动,我无法弄清楚代码的哪一部分执行此操作。你能告诉我怎么做吗?
  • 您需要在收到服务器响应后调用collectionView.reloaddata(),并将其保存在您的控制器中。

标签: ios swift uitableview uicollectionview


【解决方案1】:

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

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

【讨论】:

  • 请求是在 VC 中发出的,但 collectionview 不是由它管理的,它是由 tableViewCell 管理的,所以在收到服务器的响应后,我无法从 VC 访问 myCollection 要做到这一点,她需要问 IBOutlet 但error: Illegal Configuration: The myCollection outlet from the DetailViewController to the UICollectionView is invalid. Outlets can't be connected to repeating content. 我已经在 UITableViewCell 单元格的协议中使用了你的函数
  • 图片已加载,collectionView 已更新,但前提是立即显示。如果加载屏幕时的collectionView不在可移植性范围内 - 不会生成单元格
【解决方案2】:

解决了

第一个在 tableview 单元格上创建这个函数:

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

然后调用它

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

realPfotoCell.collectionReloadData()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多