【问题标题】:how to get tableview section when collection view cell is clicked单击集合视图单元格时如何获取表格视图部分
【发布时间】:2017-07-25 06:07:31
【问题描述】:

[![在此处输入图片描述][1]][1]

我有一个表格视图。在该表格视图中,我需要在不同部分显示集合视图。我采用原型类型表格视图单元格并显示集合视图。我对该表格视图中的不同部分使用相同的集合视图。当我单击集合视图单元格时,我得到了集合视图部分和 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

【问题讨论】:

  • 你为什么在tableViewCell里面使用collectionView,而不是collectionView with section?
  • @Balasubramanian 我对每个 tableview 部分都有不同的设计
  • 最简单的方法是在cellForRowIndexPath:方法中将tableView的indexpath保存在collectionView中,这样就可以在collectionView的action中获取indexpath
  • 你的单元应该实现一个允许你的视图控制器成为单元代表的协议。然后该单元格将成为集合视图的委托。当collection view中的item被选中时,可以调用tableview中的delegate方法,传递collection item和cell。然后,您可以确定单元格行。请参阅stackoverflow.com/questions/28659845/… 了解类似的方法(它是 Swift,但目标 C 方法是相同的)

标签: ios objective-c iphone uitableview uicollectionview


【解决方案1】:

您可以在CustomTableViewCell 中添加一个属性,例如 superIndexPath。并将属性设置为- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathlike

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

        return cell;

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

【讨论】:

  • @RajeshDharani 行“cell.superIndexPath = indexPath;”应该放在循环之外(即在重新调整单元之前)
  • cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell"]; if (cell==nil) { cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomTableViewCell"]; } cell.superIndexPath = indexPath;我犯了一些错误。该设置必须在 if{...} 之外。我已经更新了代码
  • @Land 我也试过这样,但是如果我滚动 tableview 然后单击单元格 superIndexPath 值会改变
  • @RajeshDharani 您必须在所有创建的customsTableViewCell 的位置添加“cell.superIndexPath = indexPath;”。在 =1 和 2 部分。
  • @Land 是的,我是这样添加的,但它不起作用。 cell = (CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"CustomTableViewCell"]; if (cell==nil) { cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CustomTableViewCell"]; } cell.firstIndexPath = indexPath;返回单元格;
【解决方案2】:

您可以在didSelectItemAtIndex 中使用collectionViewCellsuperView 属性并将其转换为您的CustomTableViewCell。在didSelectItemAtIndex中试试这段代码

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多