繁体   English   中英

检测UITableViewCell内部的UICollectionView上的轻击

[英]Detecting Tap on UICollectionView inside an UITableViewCell

我正在使用UITableViewCell内的UICollectionView 添加完内容后,就可以在UITableViewCell看到UICollectionView

但是现在我不知道如何检查在UITableView的哪一行内点击了UICollectionView哪个cell

因此,如果有人知道如何识别它会有所帮助。

提前致谢。

#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);
}
  1. 您可以继承UICollectionView并添加一个变量以跟踪tableViewCell的indexPath
  2. 在tableViewCell中,将collectionView类型更改为自定义collectionView
  3. 在UITableViewDatasource的“ cellForRowAtIndexPath”方法中设置变量

自定义collectionView:

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

您的自定义tableViewCell(大约)

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

UITableViewDataSource方法的实现:

-(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;
}

有点题外话,但是如果有人遇到与我自己相同的问题,则集合视图单元会碰到tableview工作内部并到达此问题以寻找答案:

确实适用于所有默认设置。 因此,请确保您的视图控制器中没有其他触摸手势(例如像我这样的轻拍手势)阻止该触摸到达集合视图单元格。

如果您有这样的手势识别器,并且想要保留它,则需要将其“ cancelTouchesInView”属性设置为false。 就这样。 您无需实现IUGestureRecognizerDelegate函数即可使其工作(尽管如果您有多个手势识别器而不仅仅是一个手势识别器,则可能会有所不同)。

这很简单。 将标签添加到“ cellForRowAtIndexPath :”方法中的UITableView每个单元格中,即

cell.tag = indexPath.row

UICollectionView内的CollectionViewCell也是如此

假设您有一个包含2行的tableView,并且分别具有2个collectionViews。

UITableView的row1将具有标签0 ,另一个将具有标签1

现在,当用户单击UITableViewCell内的collectionView内的collectionViewCell时

现在,如果您在第一个UITableViewCell中单击collectionView的第一个collectionViewCell

您将获得单元格标记为0,并且您已经知道self.tag即tableViewCell为0

现在,如果您在第二个UITableViewCell中单击collectionView的第一个collectionViewCell

您将获得单元格标记为0,并且您已经知道self.tag即tableViewCell为1

我认为这就是您所需要的。请让我知道您是否清楚..谢谢:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM