Chetan Lodhi
Chetan Lodhi

Reputation: 353

Unable send correct data from UICollectionView Embedded inside UITableView to ViewController Swift3

I am using a UICollectionView Embedded inside UITableView. I am playing music when user tap on UICollectionViewCell which is inside UITableView but when UITableView is loaded first time the data var is nil, when user tap on UICollectionViewCell the nil value is passed. when when user again tap on UICollectionViewCell than it is showing actual value of that UICollectionViewCell.

Here is my code:

//UITableViewCell

import UIKit

class CategoryRow : UITableViewCell {

 @IBOutlet var collectionView: UICollectionView!

var vcInstance: newHomeTableViewController?
var parentViewController: UIViewController? = nil


 var didSelectAction: (() -> Void )?

 }

extension CategoryRow : UICollectionViewDataSource , UICollectionViewDelegate{

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

    return imageurl.count
}


func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell  = collectionView.dequeueReusableCell(withReuseIdentifier: "newHomeCell1", for: indexPath) as! CollectionViewCell

     cell.image1.sd_setImage(with: URL(string: imageurl[indexPath.row]))

    return cell

}


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    //print("cell no: \(indexPath.row) of collection view: \(collectionView.tag)")

    didSelectAction!()

     vcInstance?.musicName = RemoteModel.sharedInstanceRemoteModel.singleGuidedTrandingTrackname[indexPath.row]
     vcInstance?.musicUrl = RemoteModel.sharedInstanceRemoteModel.singleGuidedTrandingTrackurl[indexPath.row]

    }

   }

   extension CategoryRow : UICollectionViewDelegateFlowLayout {

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let _: CGFloat = 10
    return CGSize(width : (collectionView.bounds.width)/2 - 10, height : collectionView.bounds.height - 10)
   }

  }

// TableViewController:-

import UIKit

class newTableViewController: UITableViewController {

 var musicName : String?
 var musicUrl : String?

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{

    return 70
}

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 1
}

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

         let cell : CategoryRow = tableView.dequeueReusableCell(withIdentifier: "cell")as! CategoryRow

        cell.vcInstance = self
         cell.parentViewController = self

         cell.didSelectAction = {
             self.performSegue(withIdentifier: "homePlayer", sender: indexPath)
         }

        return cell
    }

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   if segue.identifier == "homePlayer" {

        let playerVC = segue.destination as! playerViewController

        playerVC.UrlAudio = musicUrl
        playerVC.songTittle = musicName

       }
    }

  }

This is working well except when first time UITableView is loaded, the data var is nil. Thanks in advance.

Upvotes: 1

Views: 68

Answers (2)

Madhur
Madhur

Reputation: 1116

kindly refer to the code below, i have created a demo for you, matching your requirement, hope it helps!

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource{


     @IBOutlet weak var tblView: UITableView!
     @IBOutlet weak var outputLbl: UILabel!

     var content = ["Good Morning1","Good Morning2","Good Morning3","Good Morning4","Good Morning5"]

     override func viewDidLoad() {
          super.viewDidLoad()
          tblView.rowHeight = 400
     }

     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     return 01
     }

     func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath ) as! aTableViewCell
          cell.aCollection.reloadData()
          return cell
     }
}

extension ViewController : UICollectionViewDelegate, UICollectionViewDataSource {
     func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
          return content.count
     }

     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
         let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! ACollectionViewCell
          cell.aLabel.text = content[indexPath.item]
          return cell
     }

     func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
          let collectionCell = collectionView.cellForItem(at: indexPath) as! ACollectionViewCell
          let selectedData = collectionCell.aLabel.text
          outputLbl.text = selectedData
     }

}

class aTableViewCell : UITableViewCell{

     @IBOutlet weak var aCollection: UICollectionView!
}

Upvotes: 1

Madhur
Madhur

Reputation: 1116

As per your code, for the very first time, the values are nil, to avoid this, you must reload your UICollectionViewCell which is embedded in the UITableViewCell,

So to reload UICollectionViewCell, in tableView's cellForRowAt: write,

yourCollectionView.reloadData()

which should make your code work!

Upvotes: 0

Related Questions