Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iOS中点击时如何更改UITableViewHeader背景色_Ios_Objective C_Iphone_Uitableview_Uitableviewsectionheader - Fatal编程技术网

在iOS中点击时如何更改UITableViewHeader背景色

在iOS中点击时如何更改UITableViewHeader背景色,ios,objective-c,iphone,uitableview,uitableviewsectionheader,Ios,Objective C,Iphone,Uitableview,Uitableviewsectionheader,我想在每个视图中点击时更改点击的UITableViewHeader。我写了以下代码,但它根本不起作用。请帮助解决这个问题 - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 25)];

我想在每个视图中点击时更改点击的UITableViewHeader。我写了以下代码,但它根本不起作用。请帮助解决这个问题

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {

    view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 25)];
    label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, tableView.frame.size.width, 25)];
    [label setFont:CHECKOUT_HEADER_FONT];
    label.textColor = GLOBAL_PRIMARY_COLOR;
    label.textAlignment = NSTextAlignmentLeft;
    [label setText:CategoryName];
    label.backgroundColor = GLOBAL_BACKGROUND;
    [view setBackgroundColor: GLOBAL_BACKGROUND];
    [view addSubview:label];

    view.tag = section;
    UITapGestureRecognizer *headerTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
    [view addGestureRecognizer:headerTapped];

    return view;
}


- (void)sectionHeaderTapped:(UITapGestureRecognizer *)gestureRecognizer{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:gestureRecognizer.view.tag];

    // DOES NOT WORK
    UIView *header = [_productTableView headerViewForSection:indexPath.section];
    header.backgroundColor = GLOBAL_PRIMARY_COLOR;
}

您可以将此视图用于标题部分

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *tempView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 25)];
    tempView.backgroundColor=[UIColor blueColor];

    tempView.tag = section;
    UITapGestureRecognizer *headerTapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sectionHeaderTapped:)];
    [tempView addGestureRecognizer:headerTapped];

    return tempView;
}
- (IBAction)sectionHeaderTapped:(UITapGestureRecognizer *)recognizer
{
    switch (recognizer.view.tag) {
        case 0:
            recognizer.view.backgroundColor = [UIColor redColor];
            break;

        default:
            break;
    }
}

你们可以用一个按钮来代替标签和删除点击手势。若你们只想改变背景颜色,那个么按钮是最好的,否则若视图发生巨大变化,你们可以选择点击手势。但我怀疑点击手势是否只在标题上起作用,它可以在整个tableview上起作用,在这种情况下,不会调用didSelect委托函数。我只想更改标题视图。