简体   繁体   中英

image view with table view in the same view controller

I have a new controller defined as follows:

@interface view1: UITableViewController

I've created (in viewDidLoad) an image view (logo image) and added this image view as subview for view1, but the problem is that table view cells still appear behind this image view, how can completely separate the image view from the table view ?

thanks in advance.

To have a logo type view you either need to set a custom headerview for the tableview via

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

and

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;

the other method would be overriding -loadView and creating your own view that has two subviews, your imageview and a tableview.

In the first method, once your scroll some the logo will eventually disappear. The second method makes the logo static.

2 options:

1.create a UIViewController to hold your UITableViewController controller view and your imageView, then position their frame so they wont overlap

2.add the imageView as a TableView Section Header

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIImageView* imageView = [[UIImageView alloc] initWithImage:
                              [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image" ofType:@"png"]]];
    return imageView;
}

and make sure you have at least 1 section of course in:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

What is seems like you want is for your logo to be at the top, above your table view? If so then you can, in -viewDidLoad , set the tableView 's tableHeaderView to the view you want, eg:

tableView.tableHeaderView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"image"]]; // assuming ARC, else autorelease or put in variable first...

If you want it to float on top when scrolling then DanZimm's use of -tableView:viewForHeaderInSection: is what you want.

Try adding it in:

- (void)viewDidAppear:(BOOL)animated

This method is only called once you have called viewDidLoad so if you want something over everything else you might call this one or add the subview to:

[[[UIApplication sharedApplication] keyWindow] addSubview:yourView];

Hopefully it helps you.

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