繁体   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