1

[![enter image description here][1]][1]

I have a tableview.In that tableview I need to display collection view in different sections.I took proto type tableview cell and I displayed collection view.I used same collection view for different sections in that tableview. when I clicked collection view cell I got the collection view section and indexPath but how can I get the tableview section and indexPath row?

#pragma mark- UITableView Delegate & Datasource methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (section == 0) {
        return 1;
    }
    else if (section == 1) {
        return 1;
    }
    else if (section == 2) {
        return 1;
    }
    else
        return 1;

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section == 0) {
        return 128;
    }
    else if (indexPath.section == 1) {
        return 321;
    }
    else if (indexPath.section == 2) {
        return 321;
    }
    else
     return 262;
}
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{

    NSString *sectionName;
    switch (section) {
        case 0:
            sectionName = @"";
            break;
        case 1:
            sectionName = @"Features Products";
            break;
        case 2:
            sectionName = @"New Products";
            break;
        case 3:
            sectionName = @"";
            break;
        default:
            break;
    }
    return sectionName;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


    FeaturesTableViewCell *featureCell;
    CustomTableViewCell *cell;
    CustomTableViewCell *normalCell;
    FashionTableViewCell *fashionCell;

    if (indexPath.section==0) {
        featureCell = [tableView dequeueReusableCellWithIdentifier:@"FeaturesCell"];

        if (featureCell==nil) {

            featureCell = [[FeaturesTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FeaturesCell"];
        }

        return featureCell;
    }
    else if (indexPath.section==1) {

        cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell"];
        if (cell==nil)
        {
            cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomTableViewCell"];
        }
       cell.superIndexPath = indexPath;

        return cell;

    }
     else if (indexPath.section==2) {

        normalCell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell"];
        if (normalCell==nil)
        {
            normalCell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomTableViewCell"];
        }
         cell.superIndexPath = indexPath;

        return normalCell;

    }
    else{
         fashionCell = (FashionTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"FashionTableViewCell"];
         if (fashionCell==nil)
         {
             fashionCell = [[FashionTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"FashionTableViewCell"];
         }
         return fashionCell;

     }

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

    NSLog(@"UITableView Selected index----%ld",indexPath.row);


}



#import <UIKit/UIKit.h>
#import <Foundation/NSObject.h>
#import <Foundation/Foundation.h>

@interface CustomTableViewCell : UITableViewCell<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong ,nonatomic) NSIndexPath *superIndexPath;

@end


#import "CustomTableViewCell.h"

@implementation CustomTableViewCell
@synthesize superIndexPath;
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
#pragma mark- UICollectionView Delegate & Datasource methods
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    NSUInteger numberOfSections= 0;
    numberOfSections = 1;
    return numberOfSections;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSUInteger numberOfItems= 0;
    numberOfItems = 6;
    return numberOfItems;

}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    UICollectionViewCell *cell = (UICollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    UIImageView *imgView = (UIImageView *) [cell viewWithTag:1000];
    imgView.image = [UIImage imageNamed:@"slideImage1"];
    [cell.contentView addSubview:imgView];


    return cell;


}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{


    NSLog(@"Selected superIndexPath section---------%ld",(long)superIndexPath.section);
    NSLog(@"Selected superIndexPath.row---------%ld",superIndexPath.row);


}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0.0f;
}
- (UIEdgeInsets)collectionView:(UICollectionView*)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0.0f;
}

@end
Rajesh Dharani
  • 285
  • 4
  • 20
  • Why are you using collectionView inside tableViewCell, instead of collectionView with section? – Balasubramanian Jul 25 '17 at 06:15
  • @Balasubramanian I have different designs for each tableview sections – Rajesh Dharani Jul 25 '17 at 06:18
  • the simplest way would be save the indexpath of the tableView in the collectionView during cellForRowIndexPath: Method,So you can get the indexpath during collectionView actions – radkrish Jul 25 '17 at 06:23
  • Your cell should implement a protocol that allows your view controller to be the cell's delegate. The cell will then be the delegate for the collection view. When an item is selected in the collection view, you can call the delegate method in the tableview, passing the collection item and the cell. You can then determine the cell row. See https://stackoverflow.com/questions/28659845/swift-how-to-get-the-indexpath-row-when-a-button-in-a-cell-is-tapped/38941510#38941510 for a similar approach (it is Swift, but objective C approach would be the same) – Paulw11 Jul 25 '17 at 06:24

2 Answers2

1

you can add a property in the CustomTableViewCell like superIndexPath. And set the property at - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath like

cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell"];
        if (cell==nil)
        {
            cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomTableViewCell"];
        }
       cell.superIndexPath = indexPath;

        return cell;

and When you try to get the tableView indexPath in the method - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath by using self.superIndexPath.

Ax1le
  • 6,563
  • 2
  • 14
  • 61
  • @RajeshDharani the line "cell.superIndexPath = indexPath;" should be placed outside the loop(i.e before retuning the cell) – radkrish Jul 25 '17 at 07:09
  • cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell"]; if (cell==nil) { cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomTableViewCell"]; } cell.superIndexPath = indexPath; I made some mistakes. the setting must outside the if{...}. And I have updated the code – Ax1le Jul 25 '17 at 07:10
  • @Land I tried like that also but if I scroll the tableview and then clicked cell superIndexPath value is changed – Rajesh Dharani Jul 25 '17 at 07:23
  • @RajeshDharani you must add "cell.superIndexPath = indexPath; " at the location of all the customsTableViewCell created. Both at section =1 and 2. – Ax1le Jul 25 '17 at 07:30
  • @Land Yes I added like that but its not working. cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell"]; if (cell==nil) { cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomTableViewCell"]; } cell.firstIndexPath = indexPath; return cell; – Rajesh Dharani Jul 25 '17 at 07:31
  • @RajeshDharani try to post your lastest code above. Let's analyze what the problem is. – Ax1le Jul 25 '17 at 07:43
  • @RajeshDharani So the problem is when you scroll the tableview, the indexPath is not correct. Then what it should be. And what is it now. – Ax1le Jul 25 '17 at 08:00
  • @Land fist section is coming but second section index path is not coming it showing always 0 instead of 2. – Rajesh Dharani Jul 25 '17 at 08:03
  • @RajeshDharani I find the problem. You set the second cell name "cell". Please replace it with "normalCell" – Ax1le Jul 25 '17 at 08:09
  • @RajeshDharani If it helps, pls vote up. Happy coding :-) – Ax1le Jul 25 '17 at 08:18
  • @RajeshDharani If it works, also please accept my answer. I need to achieve 50 scores. – Ax1le Aug 01 '17 at 09:11
0

You can use superView property of the collectionViewCell inside didSelectItemAtIndex and cast it as your CustomTableViewCell. Try this code inside didSelectItemAtIndex

if let tableViewcell = collectionViewCell.superview?.superview as? CustomTableViewCell {
let indexPath = customTableView.indexPath(for: tableViewcell)
}
Aravind A R
  • 2,674
  • 1
  • 15
  • 25