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

时间:2012-03-16 10:06:20

标签: iphone xcode selector

我在表格标题视图部分中有这个:

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

我想传递方法sectionHeaderTapped中的节号,这样我才能识别出哪个部分被点击了。

我的方法实现如下:

-(void)sectionHeaderTapped:(NSInteger)sectionValue {
    NSLog(@"the section header is tapped ");    
}

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:15)

方法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;
    // ...
}

答案 1 :(得分:0)

备用:您可以在UIButton上添加tableHeaderView,然后点击按钮。

相关问题