2

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 for section with index 0
            NSLog(@"HELLO WORLD!!!");
            break;

        default:
            break;
    }
}
david
  • 139
  • 1
  • 4
  • 10
  • Note that 0 is the default value for the tag property. I suggest adding a constant offset. – Daniel Hepper Sep 06 '11 at 08:16
  • As Nekto said, Add recognizer to headerView, and set property headerView.userInteractionEnabled = YES; Because in my code, i added the action trigger to label, but it should in section header's view. – david Sep 06 '11 at 09:25

2 Answers2

4

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;
}
Nekto
  • 17,837
  • 1
  • 55
  • 65
  • hi Nekto, thx for your helps. I've tried this, but it seems nothing happened when i touched section header. I implement NSLog(@"Hello!"); in case 0 for testing, and it doesn't print out on console when i touch it. BTW, in "- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section " should it return to an UIView? – david Sep 06 '11 at 03:18
  • Yes, it should return UIView. Post your code of that function. – Nekto Sep 06 '11 at 06:20
  • 2
    Add `recognizer` to `headerView`, and set property `headerView.userInteractionEnabled = YES;` – Nekto Sep 06 '11 at 08:20
  • it works, thank you very much. btw, could you have a look " UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)]; " If I change to "UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 60)]" the height of section header does not change. Do you know why? – david Sep 06 '11 at 09:21
  • Yes, you should return height in method named smth like this `heightOfSection`. Try to search in docs, im writing now from iphone.. – Nekto Sep 06 '11 at 10:23
  • "- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;" thx mate – david Sep 06 '11 at 15:50
0

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");
}
Community
  • 1
  • 1
Sakiboy
  • 7,252
  • 7
  • 52
  • 69