繁体   English   中英

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

[英]How to change UITableViewHeader background color when tapped in iOS

我想在每个视图中点击时更改点击的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;
}

您可以使用此viewForHeaderInSection

-(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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM