我想讓它變成這樣,觸摸部分標題,然後跳到本節的結尾。我怎麼能這樣做?有什麼建議麼?歡呼聲我們如何才能使節標題在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是標籤屬性的默認值。我建議添加一個常數偏移量。 –
正如Nekto所說,添加識別器到headerView,並設置屬性headerView.userInteractionEnabled = YES;因爲在我的代碼中,我添加了動作觸發器來標記,但它應該在部分標題的視圖中。 – david