0

I made one collectionView inside the tableView cell

I registered all delegates and protocols and in most cases everything works fine

the problem is that the collectionView must dynamically generate cells from the server response

I installed a counter on the top of the collection that shows how many cells there are (also dynamically updated)

if the collection is in the field of view when the screen is loaded, the cells are loaded

if it is necessary to complete the collection, it is empty, although the counter indicates the presence of elements.

I can not quite understand why. Code below

code form 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
    }

code from 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

    }
Сева
  • 510
  • 1
  • 4
  • 15
  • 1
    Have you reloaded the `collectionview` after getting the response from the server ? – Marwen Doukh Jun 18 '19 at 11:01
  • you mean collectionView.reloaddata ()? – Сева Jun 18 '19 at 11:09
  • Yes `collectionView.reloaddata()` – PGDev Jun 18 '19 at 11:12
  • I do this in the `awakeFromNib ()` function After generating the cells, I do not reboot, I can not figure out what part of the code to do it. can you tell me how? – Сева Jun 18 '19 at 11:14
  • You need to call `collectionView.reloaddata()` after you receive the server response as save it in your controller. – PGDev Jun 18 '19 at 11:16
  • after server responce i do `self.myTableView.reloadRows(at: [IndexPath(row: 9, section: 0)], with: .automatic)`in VC in tableview cell i do `func setCollectionViewDataSourceDelegate(dataSourceDelegate: UICollectionViewDataSource & UICollectionViewDelegate, forRow row: Int) { MyCollection.delegate = self MyCollection.dataSource = self MyCollection.reloadData() }` – Сева Jun 18 '19 at 11:19
  • numberOfItemsInSection works when the collection is on a perforated screen. so here I see no problem. It appears then if the collectionView should update the data when the cell is not loaded yet (you need to scroll before from her) – Сева Jun 18 '19 at 11:41

2 Answers2

0

Please make sure the ReloadData() will be call when your server response will come.

   //Server Response
    MyCollection.delegate = self
    MyCollection.dataSource = self
    MyCollection.reloadData()
  • request is made in VC but collectionview is not managed by it, it is managed by tableViewCell so after receiving a response from the server, I can’t access myCollection from VC To do this, she would need to ask IBOutlet but `error: Illegal Configuration: The myCollection outlet from the DetailViewController to the UICollectionView is invalid. Outlets can't be connected to repeating content.` I already use your function in the protocols of the UITableViewCell cell – Сева Jun 18 '19 at 11:56
  • pictures are loaded, collectionView is updated but only if it is displayed immediately. if the collectionView when loading the screen is outside the field of portability - the cells are not generated – Сева Jun 18 '19 at 12:11
0

resolved

1st on tableview cell create this function:

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

then call it from

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

realPfotoCell.collectionReloadData()
Сева
  • 510
  • 1
  • 4
  • 15
  • https://stackoverflow.com/questions/35621863/how-to-reload-a-collectionview-that-is-inside-a-tableviewcell – Сева Jun 18 '19 at 13:07