如何从嵌入在UITableViewCell(Swift)中的UICollectionView单元中删除?

Chr*_*ght 0 uitableview segue uicollectionview swift

我已经使用本教程在我的ViewController中成功嵌入了一个UICollectionView内部UITableView.

下面可能会更有意义,如果你有一个快速浏览一下链接教程的代码(加上其学习过一记漂亮的东西!):

对我来说,下一步是从tableView UICollectionView内部的单元格执行segue UITableViewCell,但由于collectionView出口是在单独的View Controller中建立的,我不知道如何在主ViewController中引用它.

在TableViewCell.swift中有:

class TableViewCell: UITableViewCell {
    @IBOutlet private weak var collectionView: UICollectionView!
} 

extension TableViewCell {
    func setCollectionViewDataSourceDelegate<D: protocol<UICollectionViewDataSource, UICollectionViewDelegate>>(dataSourceDelegate: D, forRow row: Int) {
        collectionView.delegate = dataSourceDelegate
        collectionView.dataSource = dataSourceDelegate
        collectionView.tag = row
        collectionView.reloadData()
    }
}
Run Code Online (Sandbox Code Playgroud)

在ViewController.swift中,我需要能够,例如,在ViewController.swift文件中调用函数中的TableViewCell中的collectionViewprepareForSegue.我只需要用collectionView插座填补空白:

let destination = segue.destinationViewController as! SecondViewController
        let indexPaths = self.___________.indexPathsForSelectedItems()
        let indexPath = indexPaths![0] as NSIndexPath
        let arrayObject = self.arrayObjects[indexPath.row]
        destination.object = arrayObject
Run Code Online (Sandbox Code Playgroud)

'object'在SecondViewController中实现,如此var object: PFObject!.

我现在需要在上面的代码中用collectionView填补空白________,以便在SecondViewController(destinationViewController)中显示正确的'对象'

Ras*_*hid 5

  1. 从UICollectionViewCell将推送Segue添加到来自IB的YourViewController.
  2. 为segue指定一个标识符(" YourSegueIdentifier ").
  3. 在自定义UITableViewController或UIViewController中,覆盖prepareForSegue()方法.

这是:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "YourSegueIdentifier" {
        if let collectionCell: YourCollectionViewCell = sender as? YourCollectionViewCell {
            if let collectionView: UICollectionView = collectionCell.superview as? UICollectionView {
                if let destination = segue.destination as? YourViewController {
                    // Pass some data to YourViewController
                    // collectionView.tag will give your selected tableView index
                    destination.someObject = tableObjects[collectionView.tag].someObject
                    destination.productId = collectionCell.product?.id
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)