对tableview进行操作

郭琨
2023-12-01

何改变section header颜色

本文选自StackOverflow(简称:SOF)精选问答汇总系列文章本系列文章读者享外优质精彩问与答供读者习解外新技术本文读者讲解何UITableView修改section header颜色

问题:
Ilya Suzdalnitski
UITableView修改section header颜色

答案:
Alex Reynolds
希望UITableViewDelegate协议所帮助:
- (UIView ) tableView:(UITableView )tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
if (section == integerRepresentingYourSectionOfInterest)
[headerView setBackgroundColor:[UIColor redColor]];
else
[headerView setBackgroundColor:[UIColor clearColor]];
return headerView;
}
使用任何喜欢UIColor代替[UIColor redColor]能希望调整headerView尺寸
DoctorG
改变文本颜色:
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width - 10, 18)] autorelease];
label.text = @”Section Header Text Here”;
label.textColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.75];
label.backgroundColor = [UIColor clearColor];
[headerView addSubview:label];
whyoz
要忘记委托添加段代码否则某些情况视图切断或者现table面相于视图/标签高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}
Leszek ?0?3arna
想自定义header颜色做:
[[UITableViewHeaderFooterView appearance] setTintColor:[UIColor redColor]];
iOS 6.0.都用
Maulik
标题视图添加图片:
- (UIView ) tableView:(UITableView )tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
UIImageView *headerImage = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@”top-gery-bar.png”]] autorelease];

headerImage.frame = CGRectMake(0, 0, tableView.bounds.size.width, 30);

[headerView addSubview:headerImage];

return headerView;

}
William Jockusch
想建立自定义视图改变颜色(需要iOS6):
-(void) tableView:(UITableView )tableView willDisplayHeaderView:(UIView )view forSection:(NSInteger)section {
if ([view isKindOfClass: [UITableViewHeaderFooterView class]]) {
UITableViewHeaderFooterView* castView = (UITableViewHeaderFooterView*) view;
UIView* content = castView.contentView;
UIColor* color = [UIColor colorWithWhite:0.85 alpha:1.]; // substitute your color here
content.backgroundColor = color;
}
}
Dj S
见问题我认答案需要更新
涉及定义创建自定义视图iOS 6通轻松改变背景色文本色:
- (void)tableView:(UITableView *)tableView
willDisplayHeaderView:(UIView *)view
forSection:(NSInteger)section
委托
例:

  • (void)tableView:(UITableView )tableView willDisplayHeaderView:(UIView )view forSection:(NSInteger)section
    {
    // Background color
    view.tintColor = [UIColor blackColor];

    // Text Color
    UITableViewHeaderFooterView header = (UITableViewHeaderFooterView )view;
    [header.textLabel setTextColor:[UIColor whiteColor]];

    // Another way to set the background color
    // Note: does not preserve gradient effect of original header
    // header.contentView.backgroundColor = [UIColor blackColor];
    }
    orbv
    通UITableViewHeaderFooterView设置背景色已经废弃请用contentView.backgroundColor代替

 类似资料: