How to implement delegate and datasource methods for UICollectionView when it is inside a custom TableViewCell in iOS, objective C

Note : I don’t want to use any third party libraries

  • I have tableview with with a custom table view cell (table view working fine).
  • Now inside the table view cell I want to implement a collection view.
  • Now my problem is , where should I implement the delegate methods and data source methods for the collection view, which is inside the custom table view cell . below is what I have tried.

Tabel View Controller implementation

  • Apple Mach-O Linker Warning “Directory Not Found For Option…”
  • array in objective c
  • Is UIPageControl Useless By Itself?
  • What is the best way to create a custom “tickle” UIGestureRecognizer?
  • sqlite ios : attempt to write a readonly database
  • NSMutableArray and NSPredicate filtering
  • - (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 and Delegate
    methods for the CollectionView which is inside the Custom Table
    View Cell
    .

    2 Solutions Collect From Internet About “How to implement delegate and datasource methods for UICollectionView when it is inside a custom TableViewCell in iOS, objective C”

    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:

    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
        return self.mycollectionData.count;
    }
    

    Horraaaaaaaay ….. =D This is the completed answer for this.

    Table View Implementation .m file

    #import "ViewController.h"
    #import "FirstTableViewCell.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    {
        NSArray *bbarray;
    
    }
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        bbarray = [NSArray arrayWithObjects:@"33",@"44", nil];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (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"];
    
        [cell setColletionData:bbarray];
    
        return cell;
    }
    
    @end
    

    Implementation inside the Custom Table View cell

    custom table view .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
    

    custom table view .m file

    #import "FirstTableViewCell.h"
    
    @implementation FirstTableViewCell
    
    
    - (void)awakeFromNib {
        [super awakeFromNib];
    
        // These two are very important.
        self.insideCollectionView.delegate = self;
        self.insideCollectionView.dataSource = self;
    
        // Initialization code
    }
    
    - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
        [super setSelected:selected animated:animated];
    
        // Configure the view for the selected state
    }
    
    
    //this method is used to set the data , call it in cell for row at index path in table view implementation.
        - (void)setColletionData:(NSArray *)collectionData
        {
            self.mycollectionData = collectionData;
            [self.insideCollectionView reloadData];
        }
    
    
    - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
    {
        return 1;
    }
    
    - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
    {
        NSLog(@"%lu", (unsigned long)self.mycollectionData.count);
        return [self.mycollectionData count];
    }
    
    - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        UICollectionViewCell *ccell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cvcell" forIndexPath:indexPath];
        return ccell;
    }
    
    @end