How could we make the section header touchable in UITable?
I wanna make it like, touch the section header then jump to end of this section. How could I do it? Any suggestions? Cheers
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
[headerView setBackgroundColor:[[UIColor grayColor] colorWithAlphaComponent:0.7]];
UILabel *titleInSection = [[[UILabel alloc] initWithFrame:CGRectMake(50, 3, tableView.bounds.size.width/3, 20)] autorelease];
[titleInSection setBackgroundColor:[[UIColor grayColor] colorWithAlphaComponent:0]];
[titleInSection setTextColor:[UIColor whiteColor]];
if (isSectionLoad == YES){
[categoryData retain];
titleInSection.text = [categoryData objectAtIndex:section];
}
titleInSection.tag = section;
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionTapped:)];
[titleInSection addGestureRecognizer:recognizer];
[recognizer release];
[headerView addSubview:titleInSection];
return headerView;
}
- (IBAction)sectionTapped:(UITapGestureRecognizer *)recognizer
{
switch (recognizer.view.tag) {
case 0:
// do what you want f开发者_StackOverflow中文版or section with index 0
NSLog(@"HELLO WORLD!!!");
break;
default:
break;
}
}
Implement method - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
in your delegate. In this method create UIView
with needful subviews (labels, images and etc.). At the end just add UITapGestureRecognizer
to that view. And then return it it.
For example:
- (IBAction)sectionTapped:(UITapGestureRecognizer *)recognizer
{
switch (recognizer.view.tag) {
case 0:
// do what you want for section with index 0
break;
default:
break;
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *label;
label.text = [NSString stringWithFormat:@"Section %d", section];
label.tag = section;
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionTapped:)];
[label addGestureRecognizer:recognizer];
[recognizer release];
return label;
}
This is an old question, but there's a newer better answer to this question:
See this answer: https://stackoverflow.com/a/19173639/2371425
tl;dr - iOS6 gave us these new methods for changing the attributes of section headers/footers.
-(void)tableView:(UITableView *)tableView
willDisplayHeaderView:(UIView *)view
forSection:(NSInteger)
- (void)tableView:(UITableView *)tableView
willDisplayFooterView:(UIView *)view
forSection:(NSInteger)section
Use these methods to add a UITapGestureRecognizer
to the headerView.
Ex.
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionTapped:)];
[view addGestureRecognizer:recognizer];
}
- (IBAction)sectionTapped:(UITapGestureRecognizer *)recognizer {
NSLog(@"Tapped");
}
精彩评论