2016-08-17 4 views
0

segueを使用して選択したUICollectionViewCellの詳細ページを表示したいが、このセルはUITableViewCellの内側にある。問題は私が "performSegueWithIdentifier"をTableViewCell(CollectionViewが含まれています)で使用できないことです。この関数はUIViewControllerでのみ使用できますが、JSONのデータはTableViewCellにあります。私はViewControllerからこれらをアクセスする方法を知らない、あるいは多分私はTableViewCellでCollectionViewCellを選択するために他の方法を使用することができます。ここに私のコードは次のとおりです。Swift:UICollectionViewCell(UITableViewCell内)を選択したときのSegueの使い方

class TableViewCell: UITableViewCell, UICollectionViewDataSource, UICollectionViewDelegate { 

var posts1: [Posts] = [Posts]() 
var posts2: [Posts] = [Posts]() 
var categories: [Category] = [Category]() 
var selectedPost1: Posts? 

私は2つのアレイ(POST1、およびposts2)を持って、私はセグエは両方posts1とposts2配列内のすべての項目の詳細を表示したいです。

私は、機能を追加しようとしました:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    self.selectedPost1 = self.posts1[indexPath.row] 


} 

が、私は "performSegueWithIdentifier" を呼び出すことはできませんの内側。この問題を解決するための提案?ありがとう。

答えて

0

viewControllerでprepareForSegueを使用し、必要な識別子を確認してから移動したい正しいView Controllerをインスタンス化できます。あなたはこのexample

+0

あなたのUITableViewCell

extension UITableViewCell { var parentViewController: UITableViewController? { var parentResponder: UIResponder? = self while parentResponder != nil { parentResponder = parentResponder!.nextResponder() if let viewController = parentResponder as? UITableViewController { return viewController } } return nil } 

} didSelectItemAtIndexPathで、その後

に拡張子を追加する必要がチェックすることができますUITableViewControllerまたはUIViewController、しかし、私のセルは別のセルの中にありますが、どのように私はこの関数をセルの中で呼び出せますか? –

+0

あなたのセルが正しいメインUITableViewControllerが必要ですか?セルからディテールビューまでのセグを作成しましたか?私が示唆しているのは、あなたがUITableViewControllerに行くことです(performSegueIdentifierではなく)prepareForSegueをオーバーライドすることです。 prepareForSegue FUNCインスタンス オーバーライドについてこのように(:UIStoryboardSegue!送信者:セグエ!ANYOBJECT) –

+0

{ (segue.identifier == "ロードビューは"){// は、次のビュー } にデータを渡す場合}はい、Iすでにこれを行った。しかし、私はデータを渡す際に問題があります。私はインターネットからすべてのデータをダウンロードして配列に格納し、配列はUICollectionViewクラス(TableViewの内部にあります)にあります。どのように私はメインUITableViewControllerでそれらを渡すことができますか?それは私のすべてのデータが "posts1"の内部に格納されているようなものです、どうすればそれらをメインのUITableViewに渡すことができますか?ありがとう。すみません、私はSwiftには新しいです。 –

0

はあなたが私はそれがであるときにそれを使用する方法を知っている

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { 
    collectionView.deselectItemAtIndexPath(indexPath, animated: true) 
    if let tableController = parentViewController as? NameOfYourTableViewController { 
     tableController.performSegueWithIdentifier(SegueIdentifier, sender: nil) 
    } 
} 
関連する問題