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