我想要它,然后触摸部分标题然后跳到本节的结尾。我怎么能这样做?有什么建议?干杯
- (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 :(得分:4)
在您的委托中实施方法- (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;
}
答案 1 :(得分:0)
这是一个老问题,但对这个问题有更好的答案:
请参阅此答案:https://stackoverflow.com/a/19173639/2371425
tl; dr - iOS6为我们提供了这些用于更改节标题/页脚属性的新方法。
-(void)tableView:(UITableView *)tableView
willDisplayHeaderView:(UIView *)view
forSection:(NSInteger)
- (void)tableView:(UITableView *)tableView
willDisplayFooterView:(UIView *)view
forSection:(NSInteger)section
使用这些方法将UITapGestureRecognizer
添加到headerView。
实施例
- (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");
}