单击集合视图单元格时如何获取tableview部分

时间:2021-04-04 22:21:02

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

[![在此输入图片说明] [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?

我有一个tableview。在那个tableview中我需要在不同的部分显示集合视图。我使用了proto类型的tableview单元格并且我显示了集合视图。我对该tableview中的不同部分使用了相同的集合视图。当我单击集合视图单元格时,我得到了集合视图部分和indexPath但是如何获取tableview部分和indexPath行?

#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

2 个解决方案

#1


1  

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

您可以在CustomTableViewCell中添加属性,如superIndexPath。并设置属性 - (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.

当您尝试通过使用self.superIndexPath获取方法中的tableView indexPath - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath。

#2


0  

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

您可以在didSelectItemAtIndex中使用collectionViewCell的superView属性并将其强制转换为CustomTableViewCell。在didSelectItemAtIndex中尝试此代码

if let tableViewcell = collectionViewCell.superview?.superview as? CustomTableViewCell {
let indexPath = customTableView.indexPath(for: tableViewcell)
}

#1


1  

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

您可以在CustomTableViewCell中添加属性,如superIndexPath。并设置属性 - (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.

当您尝试通过使用self.superIndexPath获取方法中的tableView indexPath - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath。

#2


0  

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

您可以在didSelectItemAtIndex中使用collectionViewCell的superView属性并将其强制转换为CustomTableViewCell。在didSelectItemAtIndex中尝试此代码

if let tableViewcell = collectionViewCell.superview?.superview as? CustomTableViewCell {
let indexPath = customTableView.indexPath(for: tableViewcell)
}