如何以编程方式将图像子视图添加到tableview

时间:2011-07-13 22:20:41

标签: iphone objective-c uitableview

我是编程新手,我知道如何在.xib文件中的tableView后面添加静态图像,但不在代码中。我尝试了以下代码:

UIImage * image = [UIImage imageNamed:@"pic.png"];
[self.view addSubView: image];

但是当我写这篇文章时没有任何反应!

2 个答案:

答案 0 :(得分:3)

要显示UIImage,您必须使用UIImageView并使用相同的帧将其放在UITableView下。不要忘记将表格视图的背景设置为透明。

UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pic.png"]];
iv.frame = CGRectMake(0.0, 0.0, 300.0, 600.0);
UITableView *tv = [[UITableView alloc] initWithFrame:iv.frame style:UITableViewStylePlain];
tv.opaque = NO;
tv.backgroundColor = [UIColor clearColor];
[self.view addSubView: iv];
[self.view addSubView: tv];
[iv release];
[tv release];

答案 1 :(得分:0)

您需要使用UIImageView,而不是UIImage。此外,以编程方式添加视图时,不要忘记设置框架。

相关问题