2011-09-05 27 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"); 
} 
相關問題