1

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

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

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

4

2 回答 2

3

发生这种情况是因为您与Header 的标题headerView重叠。所以最好的方法是......也添加到你的. 并在部分中将标签文本作为标题 os 标题。UILabelheaderView

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

- (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;
}
于 2013-04-17T09:11:56.263 回答
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;
 }
于 2013-04-17T14:17:01.150 回答