Note : I don't want to use any third party libraries
- I have
tableview
with with a customtable view cell
(table view working fine). - Now inside the
table view cell
I want to implement acollection view
. - Now my problem is , where should I implement the
delegate
methods anddata source
methods for the collection view, which is inside thecustom table view cell
. below is what I have tried.
Tabel View Controller implementation
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tvcell"];
return cell;
}
working properly
now inside this FirstTableViewCell
there is a collection view
.
This is my FirstTableViweCell.h File
#import <UIKit/UIKit.h>
@interface FirstTableViewCell : UITableViewCell<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic, nullable) IBOutlet UICollectionView *insideCollectionView;
@property (strong, nonnull,nonatomic) NSArray *mycollectionData;
- (void)setColletionData :(nonnull NSArray *)collectionData;
@end
and this is my FirstTableViewCell.m File
#import "FirstTableViewCell.h"
@implementation FirstTableViewCell
- (void)awakeFromNib {
[super awakeFromNib];
self.insideCollectionView.delegate = self;
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)setColletionData:(NSArray *)collectionData
{
self.mycollectionData = collectionData;
[self.insideCollectionView reloadData];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 10;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *ccell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cvcell" forIndexPath:indexPath];
return ccell;
}
@end
following method
- (void)setColletionData:(NSArray *)collectionData
{
self.mycollectionData = collectionData;
[self.insideCollectionView reloadData];
}
I used to set the array
for collectionView
, in cellforrowatindexpath
in tableview
So what is the correct way to implement
DataSource
andDelegate
methods for theCollectionView
which is inside theCustom Table View Cell
.
Horraaaaaaaay ..... =D This is the completed answer for this.
Table View Implementation .m file
Implementation inside the Custom Table View cell
custom table view .h file
custom table view .m file
What you have is essentially correct. The only real change you need is to update the collection view data source and delegate methods in the custom cell class so they properly reference
self.mycollectionData
.For example. Change
numberOfItemsInSection
to: