簡體   English   中英

如何調用內嵌 UICollectionView 的 UITableViewCell 的 indexPath.row

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

目前,我有一個 UITableView,其中嵌入了 UICollectionView。 我有 3 個信息數組,我希望每個數組對應一個 UICollectionView,因此對應一個 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) {

    }

每個return cell?在哪里return cell? 是,我想做類似的事情:

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

我該怎么做?

你好@helloworld12345

從你的問題我得到了你想要在 tableviewcell 中收集視圖的想法

可能下面的代碼會幫助你。

首先在您的 viewController 或 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
}

在您的 tableViewCell swift 文件中:

 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
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM