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

标签 iphone xcode selector

我的表头 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/

相关文章:

iphone - 以编程方式创建的按钮不会在 iOS 的自定义 View 中触发事件

ios - 如何使用 Instagram API 获取我的关注者的关注者列表

ios - 使用 XCode 4.3.3 为 IOS 5.0 之前的用户构建应用程序

rspec - have_tag 与 have_selector

html - 从 HTML 电子邮件 iPhone 中删除日期/时间样式

ios - 将 CGGradient 转换为 CAGradientLayer

objective-c - iOS更改标签栏项颜色是否安全?

ios - 仅以英文获取用户的城市

angular - 通过 Angular 组件将函数作为参数传递给(单击)事件

iphone - UILocalizedIndexedCollat​​ion的sectionForObject :(id)object collationStringSelector:(SEL)selector method中选择器的作用是什么