2016-08-12 60 views
1

我想獲取索引時點擊UITableView標題。現在,我沒有添加UIGestureRecognizer到標題那樣:通過索引攻絲UITableViewHeader

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 

    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionTapped:)]; 

    UIView *headerView = [UIView new]; 
    headerView.backgroundColor = [UIColor grayColor]; 

    [headerView addGestureRecognizer:recognizer]; 

    // return [self.myTableView headerWithTitle:self.headers[section] totalRows:self.cells.count inSection:section]; 
    return headerView; 
} 

-(IBAction)sectionTapped:(id)sender{ 

    NSLog(@"tapped header"); 
} 

是否有簡便的方式來傳遞部分Index上自來水?

+0

是你的隔宿ture is works –

回答

1

一套標記爲您headerviewheaderView.tag = section;

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { 

    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionTapped:)]; 

    UIView *headerView = [UIView new]; 
    headerView.tag = section; 
    headerView.backgroundColor = [UIColor grayColor]; 

    [headerView addGestureRecognizer:recognizer]; 

    // return [self.myTableView headerWithTitle:self.headers[section] totalRows:self.cells.count inSection:section]; 
    return headerView; 
} 

-(IBAction)sectionTapped:(UITapGestureRecognizer *)recognizer{ 

    NSLog(@"tapped header==%d",recognizer.view.tag); 
    NSLog(@"tapped header == %ld", recognizer.view.tag); 
} 

用於替代看到this

+0

謝謝,那會做。 –

+0

'NSLog(@「tapped header ==%@」,recognizer.view.tag);'會拋出異常,因爲標記是NSInteger。 – Igor

+0

@Igor - 我更新了答案檢查一次我的兄弟, –

1

添加標籤:

recognizer.view.tag = section; 

,並得到:

- (IBAction)sectionTapped:(UITapGestureRecognizer *)recognizer{ 
    NSLog(@"tapped header %d", recognizer.view.tag); 
} 
+0

謝謝你,那工作。 –