5

In my table view, i am setting the header title. But its not visible, i have to drag down the view to see and when i release it gets hidden and only the rows are visible. I want the header to be visible by default.

Here's the table view delegates i use:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [arrayUseCases count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60; //row height
}

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

    return @"General Cases";
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"UseCasesCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    UIView *bgColorView = [[UIView alloc] init];
    // Configure the cell...

    bgColorView.backgroundColor = [UIColor colorWithRed:90.0f / 255.0f green:120.0f / 255.0f blue:190.0f / 255.0f alpha:1.0f];
    bgColorView.layer.masksToBounds = YES;
    [cell setSelectedBackgroundView:bgColorView];

    NSString *rpcName = [self.arrayUseCases objectAtIndex:[indexPath row]];

    [[cell textLabel] setText:rpcName];
    [[cell textLabel] setFont:[UIFont fontWithName:@"ChalkBoard SE" size:18]];
    cell.textLabel.textColor = [UIColor colorWithRed:90.0f / 255.0f green:120.0f / 255.0f blue:190.0f / 255.0f alpha:1.0f];


    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Clicked table view in GeneralUseCase View");
    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

    [[self navigationController] pushViewController:[storyBoard instantiateViewControllerWithIdentifier:@"LogViewID"] animated:YES];

    NSIndexPath *index = [self.tableView indexPathForSelectedRow];

    dic = [[NSDictionary alloc] initWithObjectsAndKeys:index,@"Index", nil];

    [self performSelector:@selector(postUseCaseNotification) withObject:nil afterDelay:1.0];

    //to deselect the selected row when view comes back
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:NO];

    //    [[NSNotificationCenter defaultCenter] postNotificationName:LogViewControllerGeneralUseCaseNotification object:self userInfo:dic];


}
4
  • Have you tried adding heightForHeaderInSection method and returning a height? Its under UITableViewDelegate.
    – TreeTree
    Commented Oct 25, 2013 at 10:27
  • I tried it. Adding that it further hides the visible rows. I am using this in tabview (2 tabs), same code in both the tabs. For one its fine, without the heightForHeaderInSection.
    – jxgn
    Commented Oct 25, 2013 at 10:42
  • Ok. Now i gave the return value >100. Its fine :) But I dont have this delegate in the other tableview controller and it displays the header fine. I am not sure why though ?!
    – jxgn
    Commented Oct 25, 2013 at 10:47
  • Can you vote up my answer? Commented Oct 25, 2013 at 11:17

3 Answers 3

16

Please add this method:

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

Set the table view header height

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
   return 40; // What ever height you want.
}
0
3

You forgot to implement this method (tableView:heightForHeader:) - which returns the height of the header. Otherwise it returns 0 and that's why it is not rendered:

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40; // or whatever height you want
}
1
  • @iOSDeveloper so why the answered time is later than mine?:)
    – maros
    Commented Oct 25, 2013 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.