text in header & footer of table section iPhone - 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.

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.

Related

iOS- mimic system default tableView header

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;

Remove header space between 2 section in the UITableView

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

Custom grouped UITable header in different sections

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];

how to give different header section title for different sections

- (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.

How to change text color for Section Headers in a Grouped TableView in iPhone SDK?

I am making an iPhone app where in I have a grouped TableView with headers for the sections.
Problem is that I want to change the Section Header's text color.
How can I change the text color of Section Header?
What should I do?
Add the following code to your AppDelegate class in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
method:
[[UILabel appearanceWhenContainedIn:[UITableViewHeaderFooterView class], nil] setTextColor:[UIColor whiteColor]];
If you don't want to do it app wide like in Vahan's solution, here is a solution using one of UITableViewDelegate's method :
func tableView(tableView: UITableView, willDisplayHeaderView view:UIView, forSection: Int) {
if let headerView = view as? UITableViewHeaderFooterView {
headerView.textLabel?.textColor = UIColor.redColor()
}
}
This is SURELY gonna work for you.
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *tempView=[[UIView alloc]initWithFrame:CGRectMake(0,200,300,244)];
tempView.backgroundColor=[UIColor clearColor];
UILabel *tempLabel=[[UILabel alloc]initWithFrame:CGRectMake(15,0,300,44)];
tempLabel.backgroundColor=[UIColor clearColor];
tempLabel.shadowColor = [UIColor blackColor];
tempLabel.shadowOffset = CGSizeMake(0,2);
tempLabel.textColor = [UIColor redColor]; //here you can change the text color of header.
tempLabel.font = [UIFont fontWithName:#"Helvetica" size:fontSizeForHeaders];
tempLabel.font = [UIFont boldSystemFontOfSize:fontSizeForHeaders];
tempLabel.text=#"Header Text";
[tempView addSubview:tempLabel];
[tempLabel release];
return tempView;
}
just copy and paste this function in your code.
You can implement this table view data source method:
- (UIView *)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
//create your custom label here & anything else you may want to add
return YourCustomView;
}
I built off of the answer from #Harsh.
This is the closest I could get, indistinguishable from what I can tell.
It goes in the <UITableViewDataSource> obviously.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *hView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
hView.backgroundColor=[UIColor clearColor];
UILabel *hLabel=[[[UILabel alloc] initWithFrame:CGRectMake(19,17,301,21)] autorelease];
hLabel.backgroundColor=[UIColor clearColor];
hLabel.shadowColor = [UIColor whiteColor];
hLabel.shadowOffset = CGSizeMake(0.5,1); // closest as far as I could tell
hLabel.textColor = [UIColor blackColor]; // or whatever you want
hLabel.font = [UIFont boldSystemFontOfSize:17];
hLabel.text = #"Your title here"; // probably from array
[hView addSubview:hLabel];
return hView;
}
#Harsh 's answer worked great for me, and by changing the coordinations of UILabel you can move it around. Also, I, personally thought to change the shadow offset a bit to make it more readable, but that could be a personal choice. Here's my version in case:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
// Create label with section title
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(40, -5, 300, 30)] autorelease];
//If you add a bit to x and decrease y, it will be more in line with the tableView cell (that is in iPad and landscape)
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor yellowColor];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.5., 0.5.);
label.font = [UIFont boldSystemFontOfSize:18];
label.text = sectionTitle;
// Create header view and add label as a subview
UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)]autorelease];
[view addSubview:label];
return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *myLabel = [[UILabel alloc] init];
myLabel.frame = CGRectMake(20, 8, 220, 20);
myLabel.font = [UIFont boldSystemFontOfSize:16];
myLabel.text = [self tableView:tableView
titleForHeaderInSection:section];
myLabel.backgroundColor=[UIColor grayColor];
UIView *headerView = [[UIView alloc] init];
[headerView addSubview:myLabel];
return headerView;
}