Iphone 如何在UITapGestureRecognizer中的@selector中传递参数?
我的“表格标题视图”部分中有以下内容:Iphone 如何在UITapGestureRecognizer中的@selector中传递参数?,iphone,xcode,selector,Iphone,Xcode,Selector,我的“表格标题视图”部分中有以下内容: UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(sectionHeaderTapped:)]; 我想在方法sectionHeaderTapped中传递节号,以便识别哪个节被点击 我的方法实现如下所示: -(void)sectionHeaderTapped:(NSInteger)secti
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(sectionHeaderTapped:)];
我想在方法sectionHeaderTapped
中传递节号,以便识别哪个节被点击
我的方法实现如下所示:
-(void)sectionHeaderTapped:(NSInteger)sectionValue {
NSLog(@"the section header is tapped ");
}
如何实现这一点?另一种选择:您可以在
tableHeaderView
上添加UIButton
,然后单击按钮。方法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;
// ...
}
是的,我在每个部分都使用了不同的手势,不是最好的方式,而是更简单的方式。。。谢谢