ios - 如何调用嵌入了UICollectionView的UITableViewCell的indexPath.row

标签 ios swift xcode uitableview uicollectionview

当前,我有一个嵌入了UICollectionView的UITableView。我有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?都在哪里,我想做类似的事情:
if indexPathOfTableView.row == 0 {
//designated parameters
}
else if indexPathOfTableView.row == 1 {
//designated parameters
}
else {
//designated parameters
}

我该怎么做?

最佳答案

你好@ helloworld12345

从您的问题中我得到了您想要在tableviewcell中进行collectionview的想法

可能在下面的代码会帮助您。

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

}

关于ios - 如何调用嵌入了UICollectionView的UITableViewCell的indexPath.row,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60481825/

相关文章:

swift - 静态变量在 Swift 中枚举枚举的问题

swift - 不能使用函数内部定义的结构的重载运算符

objective-c - 无法构建 Objective-C 模块 'JSQMessagesViewController'

ios - UIPageViewController : partial display when swiping right

iphone - 如何从相册中获取视频并播放

ios - Xcode 4.6 和 Xcode 5 安装在一起

ios - UITableView 单元格的动态高度问题(Swift)

ios - 整个应用程序为 "Handle No Internet"

iphone - 在 UITableView 中选择 UIImage

ios - Xcode:如何制作 launchScreen Storyboard