I use this code to set the color, etc for the Table View Section Header
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)];
// create the button object
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor whiteColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:18];
headerLabel.shadowColor = [UIColor blackColor];
headerLabel.shadowOffset = CGSizeMake(0, 1.0);
headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 44.0);
if (section == 0) {
headerLabel.text = #"Section 0 header";
[customView addSubview:headerLabel];
}
if (section == 1) {
//headerLabel.text = #"Section 1 header";
//[customView addSubview:headerLabel];
customView = nil;
tableView.tableHeaderView = nil;
}
if (section == 2) {
headerLabel.text = #"Section 2 header";
[customView addSubview:headerLabel];
}
return customView;
}
In the section 1 I don't want a header, but there is still a bigger space between section 0 and section 1.
Implement tableView:heightForHeaderInSection:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html
Related
I want to mimic programatically the exact look of this header:
So far, my best try was:
UILabel* header = [[UILabel alloc] init] ;
header.backgroundColor = [UIColor clearColor];
header.textAlignment = NSTextAlignmentCenter;
header.textColor = [[UIColor alloc] initWithRed:86 green:92 blue:112 alpha:0.1];
header.shadowColor = [UIColor darkGrayColor];
header.shadowOffset = CGSizeMake(1,0);
header.font = [UIFont boldSystemFontOfSize:18];
But it's looks like this:
Can someone please help me with the exact way to do that?
Thanks in advance!
A Norvegian guy has written a blog post about this a while ago: http://www.gersh.no/posts/view/default_styling_of_sections_headers_in_uitableview_grouped
All credits go to him for the code.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
containerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
CGRect labelFrame = CGRectMake(20, 2, 320, 30);
if(section == 0) {
labelFrame.origin.y = 13;
}
UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:17];
label.shadowColor = [UIColor colorWithWhite:1.0 alpha:1];
label.shadowOffset = CGSizeMake(0, 1);
label.textColor = [UIColor colorWithRed:0.265 green:0.294 blue:0.367 alpha:1.000];
label.text = [self tableView:tableView titleForHeaderInSection:section];
[containerView addSubview:label];
return containerView;
}
EITHER
You need to set header.frame = CGRectMake(10,1, 100, 18 );
OR
Use Datasource method of UITableView
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return #"Header"; // just pass name of your hearder
}
EDITED:
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 2, 100, 18)];
label.text= [self.listOfHeader objectAtIndex:section];
label.backgroundColor=[UIColor clearColor];
label.textAlignment=NSTextAlignmentLeft;
[view addSubview:label];
return view;
}
And also add
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 20;
}
What you can do it calculate the width of your UILabel dynamically/programmatically. Using this code :
CGSize maximumLabelSize = CGSizeMake(296,9999);
CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font
constrainedToSize:maximumLabelSize
lineBreakMode:yourLabel.lineBreakMode];
Then add 10 pixle to the width you get. Set the frame of your UILabel. And, set it following way :
header.frame = CGRectMake(0.0,5.0,expectedLabelSize.width + 10.0,expectedLabelSize.height);
header.textAlignment = NSTextAlignmentRight;
I have a question on multiple sections headers at different sections.
I have the following codes for the first section header text. however, in the second section, it displays the same header text as the first section. May I know how to modify the code so that i can display other text in the second header?
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)];
// create the button object
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor whiteColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:20];
headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 44.0);
// If you want to align the header text as centered
// headerLabel.frame = CGRectMake(150.0, 0.0, 300.0, 44.0);
headerLabel.text = #"section 1"; // i.e. array element
[customView addSubview:headerLabel];
return customView;
}
you have to use different customView for each section.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 44.0)];
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor whiteColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont boldSystemFontOfSize:20];
headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 44.0);
// If you want to align the header text as centered
// headerLabel.frame = CGRectMake(150.0, 0.0, 300.0, 44.0);
if(section == 1)
headerLabel.text = #"section 1"; // i.e. array element
else if(section == 2)
headerLabel.text = #"section 2"; // i.e. array element
else
headerLabel.text = #"section 3";//and so on
[customView addSubview:headerLabel];
return customView;
}
Instead of this
headerLabel.text = #"section 1"; // i.e. array element
Use this
headerLabel.text=[arrayContainingSectionName objectAtIndex:section];
- (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.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor colorWithHue:(136.0/360.0) // Slightly bluish green
saturation:1.0
brightness:0.60
alpha:1.0];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.font = [UIFont boldSystemFontOfSize:16];
label.text = sectionTitle;
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)];
[view autorelease];
[view addSubview:label];
return view;
}
this is for one section if i have 9 section then how can i set title
You need to implement
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
delegate method for UITableView.
This might solve your problem:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIControl *containerView = [[[UIControl alloc] initWithFrame:CGRectMake(0, 0, 320, 20)] autorelease];
UILabel *headerLabel = [[[UILabel alloc] initWithFrame:CGRectMake(10, 0, 320, 20)] autorelease];
headerLabel.textColor = [UIColor colorWithRed:50.0/255.0 green:44.0/255.0 blue:37.0/255.0 alpha:1.0];
[headerLabel setFont:[UIFont boldSystemFontOfSize:11.0]];
//[headerLabel setFont:[UIFont boldSystemFontOfSize:13.0]];
headerLabel.backgroundColor = [UIColor clearColor];
if(section == 0)
headerLabel.text = [NSString stringWithFormat:#"First Section"];
if(section == 1)
headerLabel.text = [NSString stringWithFormat:#"Second Section"];
if(section == 2)
headerLabel.text = [NSString stringWithFormat:#"Third Section"];
headerLabel.textColor = [UIColor colorWithRed:78.0/255.0 green:70.0/255.0 blue:59.0/255.0 alpha:1.0];
//headerLabel.font = [UIFont boldSystemFontOfSize:13.0];
[containerView addSubview:headerLabel];
return containerView;
}
Try this!
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *v =[[[UIView alloc] init] autorelease];
v.backgroundColor = [UIColor clearColor];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 7, tableView.bounds.size.width - 10, 18)] autorelease];
label.textColor = [UIColor whiteColor];
label.font = [UIFont boldSystemFontOfSize:15];
label.backgroundColor = [UIColor clearColor];
if (section == 1) {
label.text = #"Section 1";
[v addSubview:label];
}
else if (section == 2) {
label.text = #"Section 2";
[v addSubview:label];
}
else if (section == 3) {
label.text = #"Section 3";
[v addSubview:label];
}
return v;
}
You need to know the titles, just use an NSArray, make it global alloc and init it in viewDidLoad and release the array in dealloc.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *aView=[[[NSBundle mainBundle] loadNibNamed:#"tblHeader" owner:self options:nil]objectAtIndex:0];
[(UILabel*)[aView viewWithTag:1] setText:[[self.reports objectAtIndex:section] valueForKey:#"domain"]];
return aView;
return nil;
}
Here u can insert your custom cell also u can use the Section and get those section wise you can use switch case and easily access the section.
I've just mad Build and Analyze with my project on iphone, and i got warning from analyzer which i don't understand, this is my function:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog(#"user did select row at index: %d.%d", [indexPath section], [indexPath row]);
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSLog(#">>> Entering %s <<<", __PRETTY_FUNCTION__);
//UIView *customView;
if(section ==6){
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 36.0)];
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor whiteColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.shadowColor = [UIColor blackColor];
headerLabel.shadowOffset = CGSizeMake(0, 1);
headerLabel.font = [UIFont boldSystemFontOfSize:13];
headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 30.0);
headerLabel.text = #"Personal Data";
[customView addSubview:headerLabel];
return customView;
}
if(section ==10){
UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 36.0)];
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor whiteColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.shadowColor = [UIColor blackColor];
headerLabel.shadowOffset = CGSizeMake(0, 1);
headerLabel.font = [UIFont boldSystemFontOfSize:13];
headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 30.0);
headerLabel.text = #"Actions";
[customView addSubview:headerLabel];
[headerLabel release];
return customView;
}
else {
return nil;
}
NSLog(#"<<< Leaving %s >>>", __PRETTY_FUNCTION__);
}
and in the lines with return customView, the analyzer says:
"potential leak of an object allocated in line ###", can somebody explain me why it's hapenning?
You need:
return [customView autorelease];
You allocate your customView and don't release it anywhere in your code. Once you pass it to table view it will get retained so you can safely dispose of its ownership - by sending autorelease message.
P.S. you also forgot to release headerLabel in section == 6 branch
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.
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.