2012-03-16 54 views
7

我有這在我的表頭視圖部分:如何在UITapGestureRecognizer中的@selector中傳遞參數?

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

我想傳遞的方法sectionHeaderTapped的部分號碼,以便我能夠識別哪個部分得到挖掘。

我的方法的實現看起來是這樣的:

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

我怎樣才能做到這一點?

回答

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; 
    // ... 
} 
+0

是啊,我已經用不同的手勢每個部分,並不是最好的方法,但比較容易的方式...感謝 – 2012-03-16 12:40:32

+0

省長回答....感謝.. – 2015-04-01 11:32:36

0

替代方案:您可以在tableHeaderView上添加UIButton並點擊按鈕。