How to implement delegate and datasource methods for UICollectionView when it is inside a custom TableViewCell in iOS, objective C -
note : don't want use third party libraries
- i have
tableview
with customtable view cell
(table view working fine). - now inside
table view cell
want implementcollection view
. - now problem , should implement
delegate
methods ,data source
methods collection view, insidecustom table view cell
. below 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 firsttableviewcell
there collection view
.
this 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 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 view 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 set array
collectionview
, in cellforrowatindexpath
in tableview
so correct way implement
datasource
,delegate
methodscollectionview
insidecustom table view cell
.
what have correct. real change need update collection view data source , delegate methods in custom cell class reference self.mycollectiondata
.
for example. change numberofitemsinsection
to:
- (nsinteger)collectionview:(uicollectionview *)collectionview numberofitemsinsection:(nsinteger)section { return self.mycollectiondata.count; }
Comments
Post a Comment