简体   繁体   中英

How to call the indexPath.row of a UITableViewCell with a UICollectionView embedded within

Currently, I have a UITableView with a UICollectionView embedded in it. I have 3 arrays of information and I want each array to correspond to a UICollectionView, and therefore a UITableViewCell.

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CookieCell", for: indexPath) as? MenuCollectionViewCell
        cell?.fullView.layer.borderColor = UIColor.black.cgColor
        cell?.labelView.layer.borderColor = UIColor.black.cgColor
        cell?.fullView.layer.borderWidth = 0.3
        cell?.labelView.layer.borderWidth = 0.3
        return cell!
    }
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    }

Where each of the return cell? is, I want to do something like:

if indexPathOfTableView.row == 0 {
//designated parameters
}
else if indexPathOfTableView.row == 1 {
//designated parameters
}
else {
//designated parameters
}

How would I do this?

Hello @helloworld12345

from your question I got the idea that you want to collectionview in tableviewcell

may be below code will help you.

first in you viewController or tableviewcontroller,

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

{
  //send indexPath of this cell 
        let cell = tblView.dequeueReusableCell(withIdentifier: "tableViewCell", for: indexPath) as! tableViewCell

   //Pass your array which you want to show in collectionViewCell and then reload collection view

        cell.clnView.reloadData()

        return cell
}

In your tableViewCell swift file:

 class tableViewCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout 
{

   @IBOutlet weak var clnView: UICollectionView!

   //after awakenib method write below code

  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int 
{
    return 4 //pass your array count
}

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
    let flowayout = collectionViewLayout as? UICollectionViewFlowLayout
    let space: CGFloat = (flowayout?.minimumInteritemSpacing ?? 0.0) + (flowayout?.sectionInset.left ?? 0.0) + (flowayout?.sectionInset.right ?? 0.0)
    let size:CGFloat = (self.clnView.frame.size.width - space) / 2.0 //By this you can show two cell in one screen
    return CGSize(width: size + 60, height: (size + 200))
}


 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
{
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "clnViewCell", for: indexPath) as! clnViewCell


 // do your stuff with your 
    return cell
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM