C.Johns
C.Johns

Reputation: 10245

How to add a title to a section in a UITableView?

I have looked all over the show but cannot find how to simply define my own section title.. So far I have tried this.

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

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20; 
}

from what I have read these are the two delegate you need to add your own title.. so I am woundering what goes in the tableView:viewForHeaderInSection:

I have tried

if (section == 0){
   return @"header one";
}

but that didn't cut the mustard.. any help would be appreciated.

Upvotes: 3

Views: 7983

Answers (4)

gamozzii
gamozzii

Reputation: 3921

If you just want a title, override this method in UITableViewDataSource, rather than the two you have mentioned above.

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

Upvotes: 10

Ajay Sharma
Ajay Sharma

Reputation: 4517

Simply use this :

//This is the delegate method which you are missing in TableView.

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0){
         return @"header one";
      }

}

whereas if you wish to add the View in the Header or want to do the custom things, then you need to use this delegate method:

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

Upvotes: 2

alloc_iNit
alloc_iNit

Reputation: 5183

Following will help you out.

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
         return 1;
    }
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 
    {
         UIView *av=[[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 22)]  autorelease];
         NSString * weekdayString = [self getDayFromDate:testDate];
         UILabel *lblStr = [[UILabel alloc]initWithFrame:CGRectMake(40, 2, 100, 20)];
         lblStr.text = @"header one";
         lblStr.backgroundColor = [UIColor clearColor];
         lblStr.textColor=[UIColor whiteColor];
         lblStr.font = [UIFont boldSystemFontOfSize:15];
         [av addSubview:lblStr];
         [lblStr release];     
    }
    - (CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section {
         return 40;
    }

Upvotes: 3

Callum Jones
Callum Jones

Reputation: 595

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

Expects you to return a object that is a UIView, such as a UIView itself or something like a UILabel.

So what you could do is return a UILabel which then contains your header. Example:

UILabel *label = [[UILabel alloc] init];
[label setText:@"Section 0"];
[label autorelease];
return label;

Alternatively you could style up a whole view in the interface builder and alloc, init and return that.

If you're looking for just basic text, then gamozzii's answer is what you are looking for.

Upvotes: 2

Related Questions