xcode 如何将图像和标题添加到表视图 IOS 的标题中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11823713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:08:44  来源:igfitidea点击:

How to add image and title to header of table view IOS

iphoneobjective-ciosxcode

提问by Feroz

In table view I have created header.

在表视图中,我创建了标题。

What is the correct way to set image or title in tableView's header? What I need to do?

在 tableView 的标题中设置图像或标题的正确方法是什么?我需要做什么?

TIA

TIA

回答by Nina

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

    UIImageView *imgVew = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"header"]];
    return imgVew;
}

By this you can set image background for tableview section header!

通过这个,您可以为 tableview 部分标题设置图像背景!

回答by Neo

In the method

在方法中

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

you need to create a UIViewand add other views like UIImageView(for image), UILabel(for title) and return the UIViewat last.

您需要创建一个UIView并添加其他视图,例如UIImageView(用于图像)、UILabel(用于标题)并UIView最后返回。

回答by carmen_munich

I used this code:

我使用了这个代码:

-(void)viewDidLoad
{
   [super viewDidLoad];
   self.tableView.tableHeaderView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"header"]];
}

回答by Prathyush Kommisetty

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

    UIImage * image = [UIImage imageNamed:@"logo-big.png"];

    UIImageView * imgview = [[UIImageView alloc]initWithImage:image];

    imgview.frame = CGRectMake(0, 0, 200, 200);

    imgview.backgroundColor = [UIColor blackColor];
    return imgview;


}

回答by Hemant Dixit

Please use this code:

请使用此代码:

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

   UIImage *myImage = [UIImage imageNamed:@"loginHeader.png"];
   UIImageView *imageView = [[[UIImageView alloc] initWithImage:myImage] autorelease];
   imageView.frame = CGRectMake(10,10,300,100);

   return imageView;

}