1

各ビューでタップされたときに、タップされた 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;
}
4

1 に答える 1

1

この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;
    }
}
于 2016-03-19T12:52:49.717 に答える