2016-05-21 22 views
0

私は、uitableviewcellの中に埋め込まれたuicollectionviewセルを持っています。 uicollectionviewのデータソースとデリゲートは、TableviewControllerに接続されています。私は各テーブルビューのセルでコレクションビューを設定する方法を理解できないようです。ここに私のTableviewController内のコードは次のとおりです。UitableviewCellの内部にUICollectionViewCellを設定する方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
#warning Incomplete implementation, return the number of sections 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
#warning Incomplete implementation, return the number of rows 
    return 5; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; 




    // Configure the cell... 

    return cell; 
} 


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath]; 



    // Configure the cell 

    return cell; 
} 

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { 
#warning Incomplete implementation, return the number of sections 
    return 1; 
} 


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { 
#warning Incomplete implementation, return the number of items 
    return 13; 
} 

答えて

1

テーブルビューセルは、彼らが保持するコレクションビューのデータソースである必要があります。必要なデータをcellForRowAtIndexPath(_:)メソッドのテーブルビューセルに渡し、セルにそれぞれのコレクションビューを設定します。

+0

私が思ったことはそうですが、どうしたらいいですか? collectionviewの 'cellForItemIndexPath'はどこに実装されるのでしょうか? –

+0

これはあなたの 'UITableViewCell'サブクラスの内部で実装されます。返事が遅くなってごめん。 –

1

これは私が前に行ったことです。

collectionViewは、カスタムテーブルビューセルxibのインターフェイスビルダーにドラッグしたIBOutletです。その後、カスタムテーブルビューセルクラス内

override func layoutSubviews() { 
     super.layoutSubviews() 
     //configure collection view 
     collectionView.registerNib(UINib(nibName: "CollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "CollectionViewCell") 
     collectionView.scrollEnabled = false 
     collectionView.pagingEnabled = true 
     collectionView.dataSource = self 
     collectionView.delegate = self 
     collectionView.backgroundColor = UIColor.whiteColor() 
     //.... 
    } 

そして

PSデリゲートとデータソースのメソッドを実装します。申し訳ありませんが、それは、スウィフトに書かれていますが、簡単のObj-Cに変換することができます。

関連する問題