Pradumna Patil Pradumna Patil - 3 months ago 27
iOS Question

Detecting Tap on UICollectionView inside an UITableViewCell

I am working with the

UICollectionView
inside
UITableViewCell
. I am done with adding the contents and I am able to see the
UICollectionView
inside
UITableViewCell
.

But now I don't know how to check which
cell
of
UICollectionView
is tapped inside the which row of
UITableView
.

So if anybody knows how to recognize it pls help.

Thanks in advance.

#pragma mark
#pragma mark - UITableViewDelegate and UITableViewDatasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 15;
}

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

if (cell == nil) {
cell = [[CellForCollectionView alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellForCollectionView"];
}

cell.categoryCollectionView.delegate = self;
cell.categoryCollectionView.dataSource = self;

[cell.categoryCollectionView registerNib:[UINib nibWithNibName:@"CollectionCellForCategory" bundle:nil] forCellWithReuseIdentifier:@"CollectionCellForCategory"];
cell.lblCategoryName.text = [NSString stringWithFormat:@" Category %d",indexPath.row];

cell.selectionStyle = UITableViewCellSelectionStyleNone;
return cell;
}






#pragma mark
#pragma mark - UIcollectionViewDelegate and UIcollectionViewDatasource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}

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

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
CollectionCellForCategory *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCellForCategory" forIndexPath:indexPath];
cell.imagCategory.layer.borderColor = [[UIColor blackColor]CGColor];
cell.imagCategory.layer.borderWidth = 1.0f;
return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"%ld",(long)indexPath.row);
NSLog(@"%ld",(long)indexPath.section);
}

Answer
  1. You can subclass UICollectionView and add a variable to track the indexPath of the tableViewCell
  2. In your tableViewCell change the collectionView type to your custom collectionView
  3. Set the variable in "cellForRowAtIndexPath" method of UITableViewDatasource

Custom collectionView:

@interface IndexedCollectionView : UICollectionView
@property (nonatomic, strong) NSIndexPath* parentIndexpath;
@end

Your custom tableViewCell (approx.)

@interface CellForCollectionView : UITableViewCell
@property (nonatomic, weak) IBOutlet IndexedCollectionView* categoryCollectionView;
@end

UITableViewDataSource method implementation:

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

    if (cell == nil) {
        cell = [[CellForCollectionView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellForCollectionView"];
    }

    cell.categoryCollectionView.delegate = self;
    cell.categoryCollectionView.dataSource = self;

    [cell.categoryCollectionView registerNib:[UINib nibWithNibName:@"CollectionCellForCategory" bundle:nil] forCellWithReuseIdentifier:@"CollectionCellForCategory"];
     cell.lblCategoryName.text = [NSString stringWithFormat:@" Category %d",indexPath.row];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.categoryCollectionView.parentIndexpath = indexPath;
    return cell;
}