首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UITableView SectionHeader不显示自定义图像背景

UITableView SectionHeader不显示自定义图像背景
EN

Stack Overflow用户
提问于 2011-08-14 20:04:51
回答 3查看 2.2K关注 0票数 1

我正在修改我的TableView的节标题的外观。我设法让文本工作得很好。但是背景图像似乎根本没有显示出来。

代码语言:javascript
复制
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
    UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)] autorelease];
    UILabel *sectionTitle = [[[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 30)] autorelease];
    sectionTitle.text = [[tableDataSource objectAtIndex: section] objectForKey: @"Title"];
    sectionTitle.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
    //sectionTitle.textColor = [UIColor whiteColor];
    sectionTitle.shadowColor = [UIColor colorWithWhite:0 alpha:0.4];
    sectionTitle.shadowOffset = CGSizeMake(1, 1);
    sectionTitle.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
    //headerView.backgroundColor = [UIColor whiteColor];

    UIImageView *sectionHeaderBG = [[UIImageView alloc] init];
    UIImage *image = [UIImage imageNamed:@"CellBackgroundGrey4.png"];
    sectionHeaderBG.image = image;

    [headerView addSubview:sectionTitle];    
    [headerView addSubview:sectionHeaderBG];
    [headerView autorelease];
    return headerView;
}

我是不是漏掉了什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-08-14 20:24:05

试一试:

代码语言:javascript
复制
headerView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"CellBackgroundGrey4.png"]]; 
票数 4
EN

Stack Overflow用户

发布于 2011-08-14 20:11:19

我想你错过了设置UIImageViewframe

票数 2
EN

Stack Overflow用户

发布于 2012-04-30 14:48:19

代码语言:javascript
复制
//custom sections
- (NSString *)tableView:(UITableView *)tblView titleForHeaderInSection:(NSInteger)section {

    NSString *sectionName = nil;

    //set the table background to clear so you can see the background view behind it
    tableView.backgroundColor = [UIColor clearColor];

    //where does this go?

    UILabel *sectionHeader = [[UILabel alloc] init];
    sectionHeader.backgroundColor = [UIColor clearColor];
    sectionHeader.font = [UIFont boldSystemFontOfSize:18];
    sectionHeader.textColor = [UIColor whiteColor];

    //What is missing here?

    switch (section) {
        case 0:
            sectionName = [NSString stringWithFormat:@"Header Text 1"];
            break;
        case 1:
            sectionName = [NSString stringWithFormat:@"Header Text 2"];
            break;
        case 2:
            sectionName = [NSString stringWithFormat:@"Header Text 3"];
            break;
    }

    return sectionName;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7056698

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档