Baub
Baub

Reputation: 5044

Table HeaderView iPhone

I have a UITableView that needs to have something inserted at the top in some cases (not always).

The code (if it were to be a NORMAL) cell would be as follows

UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,98,100)];
imgView.image = [UIImage imageNamed:@"image.png"];
cell.imageView.image = imgView.image;
//sets cell text
cell.textLabel.text = @"Number";

but I was told on here that it would be easier to implement a headerView than to change the indexPath each time.

So my question is how would I implement this? Would I use - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section or would I use - (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section? Keep in mind, I have an image to place into the cell (headerView).

Thanks

Upvotes: 0

Views: 216

Answers (2)

PengOne
PengOne

Reputation: 48398

UIImageView is a subclass of UIView, so it can be returned from

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

This is what you should do since it allows full customization of the header image.

If you instead use

- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section

then the only thing you have control over is the NSString that you return. You cannot add a background, change the font color, etc. Since you have an image, use the former method.

Upvotes: 0

WrightsCS
WrightsCS

Reputation: 50697

You would use

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

titleForHeaderInSection: only sets the header as text, where as viewForHeaderInSection: will allow you to add a view, UIImageView, etc.

Upvotes: 2

Related Questions