gpt4 book ai didi

ios - 在 iOS 中点击时如何更改 UITableView 标题背景颜色

转载 作者:行者123 更新时间:2023-11-29 12:04:32 29 4
gpt4 key购买 nike

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

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

关于ios - 在 iOS 中点击时如何更改 UITableView 标题背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35545627/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com