2012-03-16 22 views
7

Tôi có điều này trong bảng phần xem tiêu đề của tôi:Làm thế nào để vượt qua đối số trong @selector trong UITapGestureRecognizer?

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

Tôi muốn vượt qua số phần trong phương pháp sectionHeaderTapped vì vậy tôi có thể nhận ra phần bị khai thác.

thực hiện phương pháp của tôi trông như thế này:

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

Làm thế nào tôi có thể đạt được điều này?

Trả lời

15

Phương pháp sectionHeaderTapped nên có một trong các dấu hiệu sau:

- (void)sectionHeaderTapped:(UITapGestureRecognizer *)sender; 
- (void)sectionHeaderTapped; 

Bạn cần phải tìm ra các tế bào đã được khai thác sử dụng tọa độ của vòi nước.

-(void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer 
{ 
    CGPoint tapLocation = [gestureRecognizer locationInView:self.tableView]; 
    NSIndexPath *tapIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation]; 
    UITableViewCell* tappedCell = [self.tableView cellForRowAtIndexPath:tapIndexPath]; 
} 

Bạn có thể nhận tiêu đề phần bằng cách sử dụng phương pháp đó. Nhưng có thể dễ dàng đính kèm trình nhận dạng cử chỉ khác nhau cho từng phần tiêu đề.

- (UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section 
{ 
    // ... 
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(sectionHeaderTapped:)]; 
    [headerView addGestureRecognizer:tapGesture]; 
    return headerView; 
} 

Và sau đó

-(void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer 
{ 
    UIView *headerView = gestureRecognizer.view; 
    // ... 
} 
+0

yea tôi đã sử dụng những cử chỉ khác nhau cho từng bộ phận, không phải là cách tốt nhất nhưng cách dễ dàng hơn ... Cảm ơn –

+0

Trưởng trả lời .... cảm ơn .. –

0

Cách thay thế: Bạn có thể thêm UIButton trên tableHeaderView và nhận được nhấp vào nút.

Các vấn đề liên quan