ios - UITableView 部分中的背景图像隐藏部分标题

标签 ios objective-c uitableview tableview

我正在使用以下方法在 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;
}

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

最佳答案

发生这种情况是因为您的 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;
}

关于ios - UITableView 部分中的背景图像隐藏部分标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16055801/

相关文章:

android - 是否需要任何配置来使用 Android 和 IOS 的 Web Api 服务(为黑莓开发)

ios - UITextField 输入不工作

iphone - 如何在iphone中以编程方式获取 "General -> Date & Time -> Set Automatically"的值?

iOS:一般从 NSObject 类序列化/反序列化复杂的 JSON

ios - 如何让 UICollectionView 单元格可以在其 setSelected : method? 中被多选

ios - 拖动 uiview iOS

ios - Web 应用程序缓存 iOS

ios - 在 iOS 中以编程方式调用号码时显示联系人姓名

ios - 搜索 Controller 搜索字典数组

ios - 将变量从 tableView 传递到 prepareForSegue?