Chandan Anand Chandan Anand - 4 months ago 5
Objective-C Question

I am having a tableview with multiple cell in it and in each cell having tableview, collectionview, tableview, like that

How to handle didSelectRowAtIndexPath so that it works on all the three different cell for each cell in it?

Preview

Flow of my code



- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([[dataArray[indexPath.row] valueForKey:@"type"] isEqual:@"Traffic" ])
{
if(!TrafficCell)
{
TrafficCell = [tableView dequeueReusableCellWithIdentifier:@"CollectionVIewTableViewCell" forIndexPath:indexPath];
NSDictionary *dict=dataArray[indexPath.row];
TrafficCell.Traffic = [dict valueForKey:@"detail"];
[TrafficCell.collectionView reloadData];
return TrafficCell;
}
return TrafficCell;
}
else if([[dataArray[indexPath.row] valueForKey:@"type"] isEqual:@"News"])
{
if(!NewsCell)
{
NewsTableViewCell *cell = (NewsTableViewCell*)[tableView dequeueReusableCellWithIdentifier:@"NewsTableViewCell" forIndexPath:indexPath];
NSDictionary *dict=dataArray[indexPath.row];
cell.News = [dict valueForKey:@"detail"];
[cell.NewsTableView reloadData];
return cell;
}
return NewsCell;

}

else
{
if(!CategoryCell)
{
CategoryCell= [tableView dequeueReusableCellWithIdentifier:@"CategoryTableViewCell" forIndexPath:indexPath];
NSDictionary *dict=dataArray[indexPath.row];
CategoryCell.Category = [dict valueForKey:@"detail"];
[CategoryCell.CategorycollectionView reloadData];
return CategoryCell;
}
return CategoryCell;
}
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{


}




Answer

You can handle tap on UITableViewCell the same as you implement in cellForRowAtIndexPath method

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
           NSDictionary *dict = dataArray[indexPath.row];
           UITableViewCell *cell = [tableView cellForRowAtIndexPath: indexPath];
           if([dict[@"type"] isEqual:@"Traffic" ]){
              //Find your collectionView in cell
              //Tap on Traffic cells
           }
           else if([dict[@"type"] isEqual:@"News"]){
              //Tap on News cells 
           }
           else {
                //Tap on other cells
           }
    }

And in cellForRowAtIndexPath you need

TrafficCell.collectionView.delegate = self;
CategoryCell.CategorycollectionView.delegate = self;

for handle tap on UICollectionViewCell and implement UICollectionViewDelegate method

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
   //Tapped on your collectionViewCell inside the uitableviewcell
}