如何在UITapGestureRecognizer中传递@selector中的参数?

Gau*_*oni 7 iphone xcode selector

我在我的表头视图部分中有这个:

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(sectionHeaderTapped:)];
Run Code Online (Sandbox Code Playgroud)

我想传递方法中的节号,sectionHeaderTapped以便我可以识别哪个部分被轻拍.

我的方法实现如下所示:

-(void)sectionHeaderTapped:(NSInteger)sectionValue {
    NSLog(@"the section header is tapped ");    
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

sch*_*sch 15

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

- (void)sectionHeaderTapped:(UITapGestureRecognizer *)sender;
- (void)sectionHeaderTapped;
Run Code Online (Sandbox Code Playgroud)

你必须弄清楚使用水龙头坐标点击的单元格.

-(void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer
{
    CGPoint tapLocation = [gestureRecognizer locationInView:self.tableView];
    NSIndexPath *tapIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation];
    UITableViewCell* tappedCell = [self.tableView cellForRowAtIndexPath:tapIndexPath];
}
Run Code Online (Sandbox Code Playgroud)

您可以使用该方法获取节头.但是可能更容易将不同的手势识别器附加到每个部分标题.

- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
    // ...
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(sectionHeaderTapped:)];
    [headerView addGestureRecognizer:tapGesture];
    return headerView;
}
Run Code Online (Sandbox Code Playgroud)

然后

-(void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer
{
    UIView *headerView = gestureRecognizer.view;
    // ...
}
Run Code Online (Sandbox Code Playgroud)