xcode 如何在 UITapGestureRecognizer 中的@selector 中传递参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9735237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass argument in @selector in UITapGestureRecognizer?
提问by Gaurav_soni
I have this in my table header view section:
我的表头视图部分中有这个:
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(sectionHeaderTapped:)];
I want to pass the section number in the method sectionHeaderTapped
so i can recognize which section got tapped.
我想在方法中传递部分编号,sectionHeaderTapped
以便我可以识别哪个部分被点击。
My method implementation looks like this:
我的方法实现如下所示:
-(void)sectionHeaderTapped:(NSInteger)sectionValue {
NSLog(@"the section header is tapped ");
}
How can I achieve this?
我怎样才能做到这一点?
回答by sch
The method sectionHeaderTapped
should have one of the following signatures:
该方法sectionHeaderTapped
应具有以下签名之一:
- (void)sectionHeaderTapped:(UITapGestureRecognizer *)sender;
- (void)sectionHeaderTapped;
You have to figure out the cell that was tapped using the coordinates of the tap.
您必须使用点击的坐标找出被点击的单元格。
-(void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer
{
CGPoint tapLocation = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *tapIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation];
UITableViewCell* tappedCell = [self.tableView cellForRowAtIndexPath:tapIndexPath];
}
You can probably get the section header using that method. But it may be easier to attach a different gesture recognizer to each section header.
您可能可以使用该方法获取部分标题。但是将不同的手势识别器附加到每个部分标题可能更容易。
- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
// ...
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(sectionHeaderTapped:)];
[headerView addGestureRecognizer:tapGesture];
return headerView;
}
And then
进而
-(void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer
{
UIView *headerView = gestureRecognizer.view;
// ...
}
回答by Maulik
An alternate : You can add UIButton
on the tableHeaderView
and get click of button.
另一种:您可以添加UIButton
在tableHeaderView
并获得点击按钮。