简体   繁体   English

如何调用内嵌 UICollectionView 的 UITableViewCell 的 indexPath.row

[英]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.目前,我有一个 UITableView,其中嵌入了 UICollectionView。 I have 3 arrays of information and I want each array to correspond to a UICollectionView, and therefore a UITableViewCell.我有 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) {

    }

Where each of the return cell?每个return cell?在哪里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你好@helloworld12345

from your question I got the idea that you want to collectionview in tableviewcell从你的问题我得到了你想要在 tableviewcell 中收集视图的想法

may be below code will help you.可能下面的代码会帮助你。

first in you viewController or tableviewcontroller,首先在您的 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
}

In your tableViewCell swift file:在您的 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.

相关问题 indexPath.row在UICollectionView中重新加载 - indexPath.row reload in UICollectionView 如何使用 indexpath.row 从 UICollectionView 中删除项目 - How to delete an item from UICollectionView with indexpath.row UICollectionView indexPath.row异常行为 - UICollectionView indexPath.row strange behaviour iOS:indexPath.row,UICollectionView和Images - iOS: indexPath.row, UICollectionView, and Images Swift 3 UITableViewCell indexPath.row搞砸了 - Swift 3 UITableViewCell indexPath.row messing up swift indexPath.row == 0对于UITableViewCell很奇怪 - swift indexPath.row==0 works weirdly for UITableViewCell 如何通过 indexPath.row 或仅行号而不是 indexpath 获得 uitableViewCell? - How can I get a uitableViewCell by indexPath.row or only row number instead of indexpath? 如何仅在UICollectionView中的indexpath.section之间排除滚动indexpath.row - How can only scroll only between Indexpath.section exclude indexpath.row in UICollectionView 如何将indexPath(而不是indexPath.row)传递到UITableViewCell的IBAction中 - How do I pass indexPath (not indexPath.row) into my IBAction for my UITableViewCell 无法在 uicollectionview、UICollectionView 的特定 indexPath.row 处为单元格设置标题 - can not set title for cell at specific indexPath.row of uicollectionview, UICollectionView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM