2016-08-15 12 views
1

コレクションビューでテーブルビューセルのデータを使用します。それらは同じコントローラ内にあります。どうやってやるの ?これを試すと、デリゲートは無限になり、何も起こりません.Notmind CellForRowAtIndexPath、numberOfRowsInSectionメソッド。デリゲートとプロトコルについてはどうすれば修正できますか?

protocol DataDelegate { 
func dataInfo(survey:Survey) 


} 

import UIKit 

class SurveyViewControllerLeftTableView: UITableView,UITableViewDataSource,UITableViewDelegate { 

var survey = [Survey]() 

var myDelegate : DataDelegate? 

override func awakeFromNib() { 
    self.dataSource=self 
    self.delegate=self 

} 



func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 


    myDelegate?.dataInfo(survey[(indexPathForSelectedRow?.row)!]) 

} 

} 


import UIKit 

class BottomCollectionView: UICollectionView,UICollectionViewDataSource,UICollectionViewDelegate,DataDelegate { 


var surveyBottomCollection = [Survey]() 



override func awakeFromNib() { 
super.awakeFromNib() 


self.delegate = self 
self.dataSource = self 
} 

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return surveyBottomCollection.count 
} 


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

    let cell : BottomCollectionViewCell = dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! BottomCollectionViewCell 

    cell.surveyQuestionNumberLabel.text = surveyBottomCollection[indexPath.row].questionNumber 
cell.surveyQuestionDescriptionLabel.text = surveyBottomCollection[indexPath.row].questionDescription 

    return cell 

} 

func dataInfo(survey: Survey) { 
print(survey.questionDescription) 

    surveyBottomCollection.append(survey) 
    self.reloadData() 
} 
+0

私はここで、 'CollectionView' Cellを' UITableView'に再利用する必要があることを理解していますが、上記のようなコードで直接動作するとは思いません。 'UITableView' Cellを作成し、Collectionビューからそのセルに必要なデータを割り当てる必要があります。 – CodeChanger

+0

私はすでにUITableView Cellを作成しましたが、どのように割り当てることができますか?簡単な例を教えていただけますか?よくわかりません。 –

+0

私にあなたに説明させてください、1つのコントローラにビューとコレクションとテーブルがあります。私はコレクションビューのセルのデータを送信するために、テーブルビューのセル内のすべての情報をクリックするとします。 –

答えて

0

あなたはUITableViewカスタムセルを作成したり、storyboardのセルを作成する必要があり、その後、あなたのテーブルビューセルへCollectionViewセルのデータを取得するためのコードの下に使用する必要が私の知識のとおり。

そしてtableview:didSelectが呼び出す任意のセルを選択し、選択機能をした時に、あなたがお互いArrayにそのテーブルビューの配列オブジェクトを追加し、あなたの中にCollectionビューのセルをその配列を使用する必要がありながら、同時に

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
      var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell 

      // Replace Beow line with your costume cell outlets 
      cell.textLabel?.text = self.items[indexPath.row] 

      //Like this Probably same like collection view or what ever you want to rename it in TableViewCell 
      cell.surveyQuestionNumberLabel.text = surveyBottomCollection[indexPath.row].questionNumber 
      cell.surveyQuestionDescriptionLabel.text = surveyBottomCollection[indexPath.row].questionDescription 

      return cell 
} 

更新:上記のコード

// Use this method to get selected object and add it into new array. 
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
      // Add Your object with indexPath.row and reload your collectionView here 
     } 

希望はあなたが望むものを達成するためのお手伝いをします。

+0

さて、私はすでにテーブルビューとコレクションビューのカスタムセルを割り当てました。 cell.surveyQuestionNumberLabel.text、cell.surveyQuestionDescriptionLabel.textをコレクションビューのセルにどのように表示できますか?私ははっきりと説明できますか? –

+0

ご存知のように、私はこのためにsegueを使用することはできません。コントローラ1台のためです。私は議定書、デリゲートが私を助けることができると思いますが、どうですか? –

+0

あなたは同じ画面にいるので、nay delegateプロトコルは必要ありませんでした。必要なのは、もう1つの配列を作成するだけです。テーブルビューのセルをクリックしてセルをクリックすると、この配列が塗りつぶされ、選択されたオブジェクトが新しい配列に追加され、コレクションビューが読み込まれます。 – CodeChanger

関連する問題