Evgeniy Kleban Evgeniy Kleban - 3 months ago 36
iOS Question

Pass index tapping UITableViewHeader

I want to get index when tap on UITableView header. Now i did add gesture recognizer to header like that:

- (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");
}


Is there are easy way to pass section index on tap?

Answer

set tag for your headerview like headerView.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);
}

for alternate see this

Comments