2016-08-12 59 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

谢谢你,那工作。 –