2011-09-05 41 views
1

我想让它变成这样,触摸部分标题,然后跳到本节的结尾。我怎么能这样做?有什么建议么?欢呼声我们如何才能使节标题在UITable中可触?

- (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; 
    } 
} 
+0

请注意,0是标签属性的默认值。我建议添加一个常数偏移量。 –

+0

正如Nekto所说,添加识别器到headerView,并设置属性headerView.userInteractionEnabled = YES;因为在我的代码中,我添加了动作触发器来标记,但它应该在部分标题的视图中。 – david

回答

3

在您的委托中实施方法- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section。用这种方法创建UIView需要的子视图(标签,图像等)。最后只需在该视图中添加UITapGestureRecognizer即可。然后归还它。

例如:

- (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; 
} 
+0

嗨Nekto,thx为你的帮助。我试过这个,但是当我碰到章节标题时似乎什么也没有发生。我实现了NSLog(@“Hello!”);如果是0则用于测试,并且当我触摸时它不会在控制台上打印出来。顺便说一句,在“ - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)部分”应该返回到UIView? – david

+0

是的,它应该返回UIView。发布你的功能代码。 – Nekto

+0

代码已更新,请查看 – david

0

这是一个老问题,但有一个新的更好的回答这个问题:

看到这个答案:https://stackoverflow.com/a/19173639/2371425

TL;博士 - iOS6的给了我们这些用于更改节头/页脚属性的新方法。

-(void)tableView:(UITableView *)tableView 
    willDisplayHeaderView:(UIView *)view 
    forSection:(NSInteger) 

- (void)tableView:(UITableView *)tableView 
    willDisplayFooterView:(UIView *)view 
    forSection:(NSInteger)section 

使用这些方法将UITapGestureRecognizer添加到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"); 
} 
相关问题