ios - 如何将图像添加到 UITableViewHeaderFooterView?

标签 ios objective-c image uitableview

谁能帮我弄清楚如何向标题 View 添加图像?我尝试使用的代码不起作用。以下是我目前所拥有的。

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{

    UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView*)view;
    if (headerView != nil)
    {

        headerView.contentView.backgroundColor = [UIColor greenColor];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(200.0f, 0.0f, 100.0f, 25.0f)];

        *imageView.image = [UIImageView = image.png];* //This line doesn't work.

        [headerView.contentView addSubview:imageView];

        UILabel *mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 25.0f)];
        mainLabel.text = [sectionTitles objectAtIndex:section];
        mainLabel.backgroundColor = [UIColor redColor];
        [headerView.contentView addSubview:mainLabel];

        UILabel *secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 26.0f, 150.0f, 20.0f)];
        secondLabel.text = @"Second Label Text";

        secondLabel.backgroundColor = [UIColor yellowColor];
        [headerView.contentView addSubview:secondLabel];

    }

}

最佳答案

请试试这个

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIImage *aImage = [UIImage imageNamed:@"myHeaderImage.png"];
    UIImageView *aImgView = [[UIImageView alloc] initWithImage:aImage];
    aImgView.frame = CGRectMake(5, 5, 310, 60);
    return aImgView;
}

或 请尝试 this , this .

关于ios - 如何将图像添加到 UITableViewHeaderFooterView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27498673/

相关文章:

ios - CAKeyframeAnimation - 停止无限循环

ios - 为什么我所有的 View Controller 都是方形的?

image - 如何编辑eps文件来更改字体?

ImageMagick/Mogrify - 以编程方式压缩图像

image - cookie 是否与图像请求一起发送?

iphone - AVPlayer后台问题

ios - didEnterRegion()/didExitRegion() 在应用程序处于后台或设备锁定时从未触发

iphone - 使用 jenkins xcode 构建插件构建时出现代码签名错误

ios - AFNetworking 显示不支持的 URL 用于 http get 操作?

objective-c - 为什么不是 drawRect : getting called?