supermonkey
supermonkey

Reputation: 655

How to add an imageView on a tableView in iOS

I am developing iOS App.

I would like to add a UIImageView to a UITableView.

I am writing down the following code, however the UIImageView is behind the UITableView.

Probably I think that after loading UITableView data, [self.view bringSubviewToFront:self.imageView]; should be implemented, however I have no idea how to do.

Could you tell me solutions?

@property (strong,nonatomic) UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

- (void)viewDidLoad
{
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(240,100,77,77)];
    self.imageView.image = [UIImage imageNamed:@"hoge"];
    [self.view addSubview:self.imageView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
     return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:  (NSInteger)section
{
     return 100;
 }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

{
     // Usual TableViewCell process
}

Upvotes: 0

Views: 1701

Answers (4)

rmp
rmp

Reputation: 3523

Try using self.tableView.tableHeaderView = headerView; where headerview is your image in a UIImageView

Upvotes: 0

Avi
Avi

Reputation: 2216

Assuming you want one element (Image view or table view) to be visible at a given time one approach can be -

[myView insertSubview:[self tableView] belowSubview:[self imageView]];

Another approach can be (assuming you add tableview and image view on top of a view) -

int tableViewIndex = [[self.view subviews] indexOfObject:[self tableView]];
[self.view insertSubview:[self imageView] atIndex: tableViewIndex];

One more -

[[self view] insertSubview:[self tableView] belowSubview:[self imageView]];

Simple one (alternative to your idea)-

[[self view] sendSubviewToBack:[self tableView]];

Upvotes: 0

casillas
casillas

Reputation: 16813

It is not so clear but you could try either directly:

cell.imageView.image = [UIImage imageNamed:@"image.png"];

or

UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imv.image=[UIImage imageNamed:@"hoge.png"];
[cell addSubview:imv];

or if you want to add an image as a background:

 UIImage *image = [UIImage imageNamed:@"hoge.png"];
 self.tableView.backgroundView = [[UIImageView alloc]initWithImage:image];

Upvotes: 0

Tcacenco Daniel
Tcacenco Daniel

Reputation: 445

If you want to set background for tableview with image view you can set like that in viewDidLoad:

 UIImage *image = [UIImage imageNamed:@"image.png"];
self.tableView.backgroundView = [[UIImageView alloc]initWithImage:image];

Upvotes: 1

Related Questions