rohit Sidpara rohit Sidpara - 1 month ago 11
iOS Question

UICollectionView delegate method "didSelectItemAtIndexPath" not called when UICollection view used in UITableviewCell

I used UICollectionView in my UITableviewCell, Everything is working fine still loading the collection view and display its cell, But after loading collection view, i unable to get "didSelectItemAtIndexPath" delegate method of collection view when tap on collection view cell.

My Table view having static cell and i put collection view in that cell like

enter image description here

Also define the delegate and datasource

enter image description here

Here is my code, data source method called but delegate not called

#pragma mark - collectionView data source


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return 10;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collCell" forIndexPath:indexPath];
UIImageView *imgDocCell = (UIImageView *)[cell viewWithTag:100];
imgDocCell.image = self.imgFileToChart;
cell.layer.borderWidth=1.0f;
cell.layer.borderColor=commonThemeColor.CGColor;
return cell;
}


#pragma mark - collectionView delegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%@",indexPath);
}

Answer

Remove outlet of collectioview and delegate and datasource too, See OUTPUT HERE

you should write your code like this

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return 1;   
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    // Here tableviewcell inside one collection view create with tag 10
    UICollectionView *coll =(UICollectionView*)[cell viewWithTag:10];
    coll.delegate = self;
    coll.dataSource = self;

    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 3;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell *cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell2" forIndexPath:indexPath];
    // in collectionview storyboard one UIImageview with tag 20
    UIImageView *imgDocCell = (UIImageView *)[cell1 viewWithTag:20];
    imgDocCell.image =[UIImage imageNamed:@"add.jpeg"];

    return cell1;
}

#pragma mark - collectionView delegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@",indexPath);
}
Comments