简体   繁体   中英

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

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?

TIA

- (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!

In the method

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

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

I used this code:

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

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;

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


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM