我的表头 View 部分中有这个:

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(sectionHeaderTapped:)];

我想在 sectionHeaderTapped 方法中传递节号,以便我可以识别哪个节被点击。

我的方法实现如下所示:
-(void)sectionHeaderTapped:(NSInteger)sectionValue {
    NSLog(@"the section header is tapped ");
}

我怎样才能做到这一点?

最佳答案

sectionHeaderTapped 方法应具有以下签名之一:

- (void)sectionHeaderTapped:(UITapGestureRecognizer *)sender;
- (void)sectionHeaderTapped;

您必须使用点击的坐标找出被点击的单元格。
-(void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer
{
    CGPoint tapLocation = [gestureRecognizer locationInView:self.tableView];
    NSIndexPath *tapIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation];
    UITableViewCell* tappedCell = [self.tableView cellForRowAtIndexPath:tapIndexPath];
}

您可能可以使用该方法获取部分标题。但是将不同的手势识别器附加到每个部分标题可能更容易。
- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
    // ...
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(sectionHeaderTapped:)];
    [headerView addGestureRecognizer:tapGesture];
    return headerView;
}

然后
-(void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer
{
    UIView *headerView = gestureRecognizer.view;
    // ...
}

关于iphone - 如何在 UITapGestureRecognizer 中的@selector 中传递参数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9735237/

10-13 01:09