iamMobile
iamMobile

Reputation: 969

text in header & footer of table section iPhone

I've a question about printing text in header and footer of table view section. I want to show some text in footer of each row, but I'm having problem doing so. When I start next section it start drawing from below of first one. To Get the idea what exactly I need, go to Settings->General->Network

In this screen, we've multiple sections and after first two sections we've footer information.

Please check this on iPhone 3G. (I'm not sure if this screen is avaiable on other variants of iPhone.)

Let me know if you need any clarification in the description.


Here are two functions implementation but it does not display as desired

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
      if (section == 1)
      {
        CGRect rect = CGRectMake(10.0f,40.0f,300.0f,250.0f);

        UILabel* label  = [[[UILabel alloc] initWithFrame:rect] autorelease];
        label.backgroundColor   = [UIColor clearColor];
            label.text      = NSLocalizedString(@"MessageHeader","");;
            label.baselineAdjustment= UIBaselineAdjustmentAlignCenters;
            label.lineBreakMode =  UILineBreakModeWordWrap;
            label.textAlignment = UITextAlignmentCenter;
            label.shadowColor   = [UIColor whiteColor];
            label.shadowOffset  = CGSizeMake(0.0, 1.0);
            label.font      = [UIFont systemFontOfSize:14];
            label.textColor     = [UIColor darkGrayColor];
            label.numberOfLines = 0;


            UIView *view = [[[UIView alloc] initWithFrame:rect] autorelease];

            [view addSubview:label];

            return view;
        }
    }   
    return nil;     
}


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

   if (section == 1) 
   {
                          CGRect rect       = CGRectMake(10.0f,80.0f,300.0f,250.0f); 

                         UILabel* label = [[[UILabel alloc] initWithFrame:rect] autorelease];
                      label.backgroundColor = [UIColor clearColor];
                      label.text        = NSLocalizedString(@"MessageFooter","");
                      label.baselineAdjustment= UIBaselineAdjustmentAlignCenters;
            label.lineBreakMode     =  UILineBreakModeWordWrap;
            label.textAlignment     = UITextAlignmentCenter;
            label.shadowColor       = [UIColor whiteColor];
            label.shadowOffset      = CGSizeMake(0.0, 1.0);
            label.font          = [UIFont systemFontOfSize:14];
            label.textColor         = [UIColor darkGrayColor];
            label.numberOfLines = 0;
            UIView *view = [[[UIView alloc] initWithFrame:rect] autorelease];
            [view addSubview:label];

            return view;
        }
    }
    return nil;
}

What's wrong here? Why Header & footer text goes all the way to bottom of all the sections.

Upvotes: 3

Views: 6882

Answers (1)

Dave DeLong
Dave DeLong

Reputation: 243146

The UITableViewDelegate protocol allows you to return views for the header and footer of a section. Simply implement this method and return a UILabel filled with the text you want to display.

Upvotes: 3

Related Questions