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

时间:2015-12-28 00:51:04

标签: swift uitableview uicollectionview segue

我已使用this tutorial成功将UICollectionView嵌入到我的ViewController中的UITableView内。

如果你快速浏览一下链接教程的代码(以及一个非常好的东西也可以学习它),以下内容可能会更有意义。:

我的下一步是从tableView的UICollectionView内的UITableViewCell单元格中执行segue,但由于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()
    }
}

在ViewController.swift中,我需要能够,例如,调用ViewController.swift中prepareForSegue函数中TableViewCell中的 collectionView 。文件。我只需要用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

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

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

4 个答案:

答案 0 :(得分:5)

  1. 将来自UICollectionViewCell的Push Segue添加到IB中的 YourViewController
  2. 为您的segue提供一个标识符(“ YourSegueIdentifier ”)。
  3. 在自定义UITableViewController或UIViewController中,覆盖 prepareForSegue()方法。
  4. 这是:

    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
                    }
                }
            }
        }
    }
    

答案 1 :(得分:2)

您好我有同样的问题

这就是我所做的:

<强> 1。在第一个视图控制器的扩展名中为单元格设置标记

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("advertisementCollectionCell",forIndexPath: indexPath) as! AdvertisementCollectionViewCell

        // set your cell information

        cell.tag = indexPath.row

        return cell
    }

<强> 2。在故事板上从UIcollectionViewCell到新的视图控制器

执行segue

第3。在第一个TableViewController的 prepareForSegue 中:

 else if segue.identifier == "identifier"
    {

        let destinationViewController = segue.destinationViewController as! DestinationViewController
        if let cell = sender as? MyCollectionViewCell
        {
            let indexPath = cell.tag
        // use indexPath :D
        }
    }

答案 2 :(得分:0)

我刚玩了你附上的代码。我手动在故事板中添加了segue。它很容易打电话。

1)只需将viewcontroller添加到故事板。 2)Controll +拖放来自collectionview单元格的segue

enter image description here

答案 3 :(得分:0)

在您的代码中,对CollectionView的引用在TableViewCell中连接 因此,您可以通过TableViewCell引用它并设置数据源和委托 要在代码中添加connect,必须选择带有RightButton的CollectionView并拖放到@IBOutlet private weak var collectionView:UICollectionView!
单击CollectionViewCell时,委托将调用UICollectionViewDelegate的collectionView(collectionView:UICollectionView,didselecteditemAtIndexPath indexPath:NSIndexPath)函数。
那时,你可以通过在这个函数中调用performSegue来实现 当您在代码中编写时,tableview的索引由标记设置,因此您可以使用collectionView.tag()逐个标记获取索引。 代码如下 var tableViewIndex = collectionView.tag()
因此,您可以使用tableViewIndex和indexPath引用正确的单元格。

<!DOCTYPE html>
<html>
  <head>
    <title>Angular 2 QuickStart</title>
    <!-- bootstrap -->
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

    <!-- 1. Load libraries -->
        <script src="https://rawgithub.com/systemjs/systemjs/0.19.6/dist/system.js"></script>
        <script src="https://code.angularjs.org/tools/typescript.js"></script>
        <script src="https://code.angularjs.org/2.0.0-beta.0/angular2-polyfills.js"></script>
        <script src="https://code.angularjs.org/2.0.0-beta.0/Rx.js"></script>
        <script src="https://code.angularjs.org/2.0.0-beta.0/http.dev.js"></script>
        <script src="https://code.angularjs.org/2.0.0-beta.0/angular2.dev.js"></script>

    <!-- 2. Configure SystemJS -->
    <script>
        System.config({
            transpiler: 'typescript', 
            typescriptOptions: {emitDecoratorMetadata: true}, 
            packages: {'app': {defaultExtension: 'ts'}} 
        });
        System.import('app/boot')
            .then(null, console.error.bind(console));
    </script>

  </head>

  <!-- 3. Display the application -->
  <body>
    <my-app>Loading...</my-app>
  </body>

</html>
相关问题