OStack程序员社区-中国程序员成长平台

标题: iphone - 我们如何让 UITable 中的部分标题可触摸? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 17:21
标题: iphone - 我们如何让 UITable 中的部分标题可触摸?

我想让它喜欢,触摸部分标题,然后跳到本部分的末尾。我怎么能做到?有什么建议么?干杯

- (UIView *) tableViewUITableView *)tableView viewForHeaderInSectionNSInteger)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 actionselector(sectionTapped];
    [titleInSection addGestureRecognizer:recognizer];
    [recognizer release];

    [headerView addSubview:titleInSection];
    return headerView;
}

- (IBAction)sectionTappedUITapGestureRecognizer *)recognizer
{
    switch (recognizer.view.tag) {
        case 0:
            // do what you want for section with index 0
            NSLog(@"HELLO WORLD!!!");
            break;

        default:
            break;
    }
}



Best Answer-推荐答案


在您的委托(delegate)中实现方法 - (UIView *)tableViewUITableView *)tableView viewForHeaderInSectionNSInteger)section。在此方法中,创建带有必要 subview (标签、图像等)的 UIView。最后只需将 UITapGestureRecognizer 添加到该 View 。然后把它还回去。

例如:

- (IBAction)sectionTappedUITapGestureRecognizer *)recognizer
{
    switch (recognizer.view.tag) {
        case 0:
            // do what you want for section with index 0
            break;

        default:
            break;
    }
}

- (UIView *)tableViewUITableView *)tableView viewForHeaderInSectionNSInteger)section
{
    UILabel *label;
    label.text = [NSString stringWithFormat"Section %d", section];
    label.tag = section;
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self actionselector(sectionTapped];
    [label addGestureRecognizer:recognizer];
    [recognizer release];
    return  label;
}

关于iphone - 我们如何让 UITable 中的部分标题可触摸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7307337/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4