【问题标题】:Background Images In UITableView Sections Hides Section TitlesUITableView 部分中的背景图像隐藏部分标题
【发布时间】:2013-04-17 08:59:07
【问题描述】:

我正在使用以下方法在UITableView的section的标题中设置标题,效果很好。

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSString *sectionHeader = nil;

    if (section == 0)
    {
        sectionHeader = nil;
    }
    if(section == 1)
    {
        sectionHeader = @"Categories";
    }
    if (section == 2) {
        sectionHeader = @"General";
    }
    return sectionHeader;
}

使用以下方法,我在部分中设置标题的高度,效果很好。

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

但是,一旦我使用以下方法在部分标题中设置背景图像,部分标题中的标题就会消失并变为空,但当然是图像位置。

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)];
    UIImageView *headerImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"section_background.png"]];

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

    [headerView addSubview:headerImage];

    return headerView;
}

如何在节标题中设置标题而不删除其中的背景图像。有没有好心人指出来。提前致谢。

【问题讨论】:

标签: ios objective-c uitableview tableview


【解决方案1】:

这是因为您的headerView 与您的标题标题重叠。所以最好的方法是......也将UILabel添加到您的headerView。并在部分中将标签文本作为标题 os 标题。

我尝试使用以下代码 .. 使用它作为您的方式,可能对您的情况有所帮助。

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)];
    UIImageView *headerImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]]; //set your image/

    UILabel *headerLbl = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 205, 15)];//set as you need
    headerLbl.backgroundColor = [UIColor clearColor];
    headerLbl.text = [self.listOfHeader objectAtIndex:section];
    [headerImage addSubview:headerLbl];

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

    [headerView addSubview:headerImage];

    return headerView;
}

【讨论】:

    【解决方案2】:

    另一种方法是直接从图像 H、W 设置 CGRect 的尺寸

     - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
     UIImage *myImage = [UIImage imageNamed:@"header.png"];
     UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, myImage.size.width, myImage.size.height)];
    
     UIImageView *imageView = [[UIImageView alloc] initWithImage:myImage];
     imageView.frame = CGRectMake(0,0,myImage.size.width, myImage.size.height);
    
      headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 5, 300, 25)];
      headerLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:15.0];
      headerLabel.backgroundColor = [UIColor clearColor];
    
      NSString *myString = [NSString stringWithFormat:@"This is Header ;)"];
      NSLog(@" headerLabel.text: %@", myString);
      headerLabel.text = myString;
     [imageView addSubview:headerLabel];
    
     [tempView addSubview:imageView];
    
     return tempView ;
    
     }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
     return 50;
     }
    

    【讨论】: