Godric
Godric

Reputation: 59

Setting header for only one uitableview

I need help. I have three UITableViews in one UIView. However, I only want one UITableView to have a header. How can I say it in code? I already have the following code. Thanks guys.

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {   
    if (tableView == Table1) {
        return @"My Header Text";
    } else if (tableView == Table2) {
        return nil;
    } else if (tableView == Table3) {
        return nil;
    }
}   

Upvotes: 0

Views: 595

Answers (3)

lreddy
lreddy

Reputation: 479

Try this

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
 UILabel *label = [[[UILabel alloc] init] autorelease];
 label.frame = CGRectMake(20, 6, 300, 30);
 label.textColor = [UIColor blackColor];
 label.font = [UIFont boldSystemFontOfSize:16];
 label.text = sectionTitle;

  UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)];
  [view addSubview:label];
  return view;
  }

Upvotes: 0

Dinesh Raja
Dinesh Raja

Reputation: 8501

The code you provided will be add a header view for a certain section only. So If you have two or more section then you need to write extra logic to set the header view for each section.

Here it is an easy way to set the tableHeaderView for single table is.

UILabel *label = [[UILabel alloc]initWithFrame:CGRectZero];
label.text = @"my table header";
table1.tableHeaderView = label;

Upvotes: 2

MANIAK_dobrii
MANIAK_dobrii

Reputation: 6032

If that's not working you could also try to return zeroes for header's heights as well:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (tableView == Table1) 
    {
        return 50.0; // or what's the height?
    } 
    else
    {
         return 0.0;
    }
}

Upvotes: 1

Related Questions