learningtech
learningtech

Reputation: 33725

Change the background color of header in a UITableView

I am trying to change the background color of the header in the UITableViewController, which actually has it's own *.h/m file. I my .m file, I pasted the following code as suggested by several stackoverflow answers:

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
    if (section == 0)
        [headerView setBackgroundColor:[UIColor redColor]];
    else 
        [headerView setBackgroundColor:[UIColor clearColor]];
    return headerView;
}

But the result is the following:

enter image description here

I want the blue header to be red. I don't understand why there is a red bar between the header and the first cell.

Additional Details In case this comment is useful: I'm new to xcode 4.2. What I did was go to storyboard, drag a navigation controller, drag a table view. Go to left hand explorer bard and add a new file with name MyUITableViewController that extends UITableviewController. Pasted my function above into the MyUITabelViewController.m file. Then i implemented the heightforheaderinsection funciton to return a CGfloat value (and all that did afterwards was increase the space between the blue header and the first cell). Go back to storyboard, left click my tableviewcontroller and make sure it points to my MyUITableViewController class. What steps am I missing?

Upvotes: 1

Views: 7580

Answers (3)

cabhara
cabhara

Reputation: 481

The blue header shown is the navigation controller header.

Upvotes: -1

Garoal
Garoal

Reputation: 2374

Apple's UITableViewDelegate class reference, on method - tableView:viewForHeaderInSection::

This method only works correctly when tableView:heightForHeaderInSection: is also implemented.

Did you implement that method?

EDIT: after read your comments, and knowing that you're using storyboard, you have an easy way to create a custom background for the whole table:

1) Create a TableViewController on your storyboard

2) Drag a UIView:

UIView

3) Drop the UIView into your table view's header, here:

Dropping view into tableView header

4) You have now a view on the header that you can resize, change color, add, e.g., an UILabel...:

TableView with header header with label

Upvotes: 3

Nitin
Nitin

Reputation: 7471

Use tablview delegate method for customize tablviewheader. I show here some code use it...

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

    UIView *headerView = [[UIView alloc] init];
    UIImageView *tempimage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 5, 300,34)];// you can also use image view to show your required color.
    tempimage.image = [UIImage imageNamed:@"yourimage.png"];
    [headerView addSubview:tempimage];
    [headerView addSubview:headername];
    return  headerView;

  }

Hope, this will help you..

Upvotes: 0

Related Questions