i used the follow code to set the title at the group tableview title header,but default the text is AlignmentLeft,how to AlignmentCenter?
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if(section==0){
return NSLocalizedString(#"more_titlehead_one", nil);
}else if (section==1){
return NSLocalizedString(#"more_titlehead_two", nil);
}else{
return NSLocalizedString(#"more_titlehead_three", nil);
}
}
Try like this,
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel * sectionHeader = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
sectionHeader.backgroundColor = [UIColor clearColor];
sectionHeader.textAlignment = UITextAlignmentCenter;
sectionHeader.font = [UIFont boldSystemFontOfSize:10];
sectionHeader.textColor = [UIColor whiteColor];
switch(section) {
case 0:sectionHeader.text = #"TITLE ONE"; break;
case 1:sectionHeader.text = #"TITLE TWO"; break;
default:sectionHeader.text = #"TITLE OTHER"; break;
}
return sectionHeader;
}
set some default height for Header,
- (CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section {
switch(section) {
case 0:
case 1:
default:return 20;
}
}
Use the below method to set the UIView item for the header in each section:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerVw = [[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 30)] autorelease];
headerVw.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"title_bg.png"]]; // set color of header
UILabel *itemlbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 320, 20)];
if(section==0){
itemlbl.text = NSLocalizedString(#"more_titlehead_one", nil);
}else if (section==1){
itemlbl.text = NSLocalizedString(#"more_titlehead_two", nil);
}else{
itemlbl.text = NSLocalizedString(#"more_titlehead_three", nil);
}
itemlbl.textAlignment = UITextAlignmentCenter;
itemlbl.backgroundColor = [UIColor clearColor];
itemlbl.textColor = [UIColor whiteColor];
itemlbl.font = [UIFont boldSystemFontOfSize:14];
[headerVw addSubview:itemlbl];
[titlelbl release];
return headerVw;
}
Best of luck
It is very simple use UILabel with its Alignment center. in viewForHeaderInSection
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel *lbl = [[[UILabel alloc] init] autorelease];
lbl.textAlignment = UITextAlignmentCenter;
lbl.backgroundColor=[UIColor clearColor];
lbl.text = #"Header Title";
return lbl;
}
Related
I want to achieve something like this
mainly for Instruction purpose, is there a UITableView method I am unaware of, or there is some sort of trick.
This is written in Header and Footer of UItableView. So customize headerView and footerView of sections in table.
Use these methods :
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
Hope it helps you.
If you want to make same kind of view (as per above shown image),put a label in the footer of the section,make the background of the label transparent and set its frame according to your choice and add the text which you want to put over there. let me give you a code snippet which might help you.
UILabel *tableFooter = [[UILabel alloc] initWithFrame:footerRect];
tableFooter.textColor = // add color of your choice
tableFooter.backgroundColor = // add color of your choice
tableFooter.opaque = YES;
tableFooter.font = [UIFont boldSystemFontOfSize:15];
tableFooter.text = #" add text of your choice";
self.theTable.tableFooterView = tableFooter;
[tableFooter release];
Put the above code in this method :
- (UIView *) tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
Check out this delegate method:
- (NSString *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
You following methods its works for me in my app :
- (void)viewDidLoad {
[super viewDidLoad];
/* ios 7 Change */
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7)
{
self.edgesForExtendedLayout = UIRectEdgeNone;
self.edgesForExtendedLayout = UIRectEdgeNone;
self.tableview.separatorInset = UIEdgeInsetsZero;
[self.tableview setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *header;
float start = 10.0, startLabel = 10.0;
if ([UICommonUtils isiPad])
{
startLabel = 50.0;
} else {
startLabel = 10.0;
}
CGRect rectFrame = [UIScreen mainScreen].applicationFrame;
UIView* customView = [[[UIView alloc]
initWithFrame:CGRectMake(start, 0.0, rectFrame.size.width - 20.0, 80.0)]
autorelease];
/*
customView.backgroundColor =
[UIColor colorWithRed:.6 green:.6 blue:1 alpha:.9];
*/
UILabel * headerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
//UILabel * headerLabel = [[[UILabel alloc] initWithStyle:UITableViewCellStyleDefault] autorelease];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
//headerLabel.textColor = [UIColor darkGrayColor];
headerLabel.textColor = [UIColor colorWithRed:0.298039 green:0.337255 blue:0.423529 alpha:1];
headerLabel.shadowColor = [UIColor whiteColor];
headerLabel.shadowOffset = CGSizeMake(0.0, 1.0);
headerLabel.font = [UIFont boldSystemFontOfSize:17.0];
//headerLabel.frame = CGRectMake(startLabel, 0, rectFrame.size.width - 60.0, 20.0);
headerLabel.frame = CGRectMake(startLabel, 10, rectFrame.size.width - 35.0, 20.0);
headerLabel.textAlignment = UITextAlignmentLeft;
headerLabel.lineBreakMode = UILineBreakModeWordWrap;
headerLabel.text = header;
headerLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
UILabel * footerLabel = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
//UILabel *footerLabel = [[[UILabel alloc] initWithStyle:UITableViewCellStyleDefault] autorelease];
footerLabel.backgroundColor = [UIColor clearColor];
footerLabel.opaque = NO;
//footerLabel.textColor = [UIColor darkGrayColor];
footerLabel.textColor = [UIColor colorWithRed:0.298039 green:0.337255 blue:0.423529 alpha:1];
footerLabel.shadowColor = [UIColor whiteColor];
footerLabel.shadowOffset = CGSizeMake(0.0, 1.0);
footerLabel.font = [UIFont systemFontOfSize:14.0];
float height;
if ([UICommonUtils isiPad])
height = 40.0;
else
height = 50.0;
footerLabel.frame = CGRectMake(startLabel, 20, rectFrame.size.width - 60.0, height);
footerLabel.textAlignment = UITextAlignmentLeft;
footerLabel.lineBreakMode = UILineBreakModeWordWrap;
footerLabel.baselineAdjustment = UIBaselineAdjustmentNone;
footerLabel.numberOfLines = 0;
footerLabel.text = detail;
[customView addSubview:headerLabel];
[customView addSubview:footerLabel];
return customView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if ([UICommonUtils isiPad]) return 60.0;
else return 70.0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger)section
{
return kProductHeader;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
/* ios 7 Change */
if ([cell respondsToSelector:#selector(tintColor)]) {
if (tableView == self.tableview) {
CGFloat cornerRadius = 5.f;
cell.backgroundColor = UIColor.clearColor;
CAShapeLayer *layer = [[CAShapeLayer alloc] init];
CGMutablePathRef pathRef = CGPathCreateMutable();
CGRect bounds = CGRectInset(cell.bounds, 10, 0);
BOOL addLine = NO;
if (indexPath.row == 0 && indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
CGPathAddRoundedRect(pathRef, nil, bounds, cornerRadius, cornerRadius);
} else if (indexPath.row == 0) {
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds));
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds), CGRectGetMidX(bounds), CGRectGetMinY(bounds), cornerRadius);
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds));
addLine = YES;
} else if (indexPath.row == [tableView numberOfRowsInSection:indexPath.section]-1) {
CGPathMoveToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMinY(bounds));
CGPathAddArcToPoint(pathRef, nil, CGRectGetMinX(bounds), CGRectGetMaxY(bounds), CGRectGetMidX(bounds), CGRectGetMaxY(bounds), cornerRadius);
CGPathAddArcToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMaxY(bounds), CGRectGetMaxX(bounds), CGRectGetMidY(bounds), cornerRadius);
CGPathAddLineToPoint(pathRef, nil, CGRectGetMaxX(bounds), CGRectGetMinY(bounds));
} else {
CGPathAddRect(pathRef, nil, bounds);
addLine = YES;
}
layer.path = pathRef;
CFRelease(pathRef);
layer.fillColor = [UIColor colorWithWhite:1.f alpha:0.8f].CGColor;
if (addLine == YES) {
CALayer *lineLayer = [[CALayer alloc] init];
CGFloat lineHeight = (1.f / [UIScreen mainScreen].scale);
lineLayer.frame = CGRectMake(CGRectGetMinX(bounds)+10, bounds.size.height-lineHeight, bounds.size.width-10, lineHeight);
lineLayer.backgroundColor = tableView.separatorColor.CGColor;
[layer addSublayer:lineLayer];
}
UIView *testView = [[UIView alloc] initWithFrame:bounds];
[testView.layer insertSublayer:layer atIndex:0];
testView.backgroundColor = UIColor.clearColor;
cell.backgroundView = testView;
}
}
}
In my tableview, i need the header only when something is typed in the search box. And no header in normal view. what should i return if i don need header?
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *section1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 22)];
if(tableView==ExpTableView)
{
[section1 setBackgroundColor:[UIColor colorWithRed:241.0f/255.0f green:57.0f/255.0f blue:130.0f/255.0f alpha:1.0f]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width - 10, 18)];
if (isSearchOn)
{
label.text = [NSString stringWithFormat:#"Search Results for '%#'", searchTextValue];
label.textColor = [UIColor whiteColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.backgroundColor = [UIColor clearColor];
[section1 addSubview:label];
return section1;
}
else
{
return nil;
}
}
return nil;
}
its the
if(tableView==ExpTableView)
that's breaking it. you're assuming it always is but it is not when you're searching. when searching it will never go in to that block
you should have two ifs. one is that one and one that checks to see if the table is the searchview
something like:
if(tableView == [[self searchDisplayController] searchResultsTableView])
so from my take on what you're trying to do:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(tableView == [[self searchDisplayController] searchResultsTableView]) {
UIView *section1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 22)];
[section1 setBackgroundColor:[UIColor colorWithRed:241.0f/255.0f green:57.0f/255.0f blue:130.0f/255.0f alpha:1.0f]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width - 10, 18)];
label.text = [NSString stringWithFormat:#"Search Results for '%#'", searchTextValue];
label.textColor = [UIColor whiteColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.backgroundColor = [UIColor clearColor];
[section1 addSubview:label];
return section1;
}
return nil;
}
this method works hand in hand with the following method that you need to also alter so that you get the functionality that you're after:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if(tableView == [[self searchDisplayController] searchResultsTableView])
return /*DESIRED_HEIGHT_OF_HEADER*/;
}else {
return 0;
}
}
I have some weird behavior on the iPad that I am not getting on the iPhone.
I have a Grouped table view that has sections and headers for the sections, the problem is that on the iPad the top most section's header is not displayed, when scrolling down the table the sections header appears for a short while just before going of screen.
Before scrolling
http://desmond.imageshack.us/Himg525/scaled.php?server=525&filename=screenshot2012051410074.png&res=landing http://desmond.imageshack.us/Himg525/scaled.php?server=525&filename=screenshot2012051410074.png&res=landing
After scrolling
http://desmond.imageshack.us/Himg59/scaled.php?server=59&filename=screenshot2012051410074.png&res=landing http://desmond.imageshack.us/Himg59/scaled.php?server=59&filename=screenshot2012051410074.png&res=landing
The code for creating the headers of the sections:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil || [sectionTitle isEqualToString:#""]) {
return nil;
}
// Create label with section title
UILabel *label = [[UILabel alloc] init] ;
label.frame = CGRectMake(12, 0, 300, 30);
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.font = [UIFont boldSystemFontOfSize:16];
label.text = sectionTitle;
UIImage *img = [UIImage imageNamed:#"header"];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 300, 44)];
imgView.image = img;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 44)];
[view addSubview:imgView];
[view addSubview:label];
return view;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
int aSection = [[self.sectionsToDisplay objectAtIndex:section] integerValue];
return [self.groupHeadings objectAtIndex:aSection];
}
My TableView's code:
tableViewResult = [[UITableView alloc] initWithFrame:mainView.frame style:UITableViewStyleGrouped];
tableViewResult.separatorColor = [UIColor clearColor];
[mainView addSubview:tableViewResult];
I set the delegate and datasource in another method as I first do a web request before loading any data into the table, ie when the web request is done I do:
tableViewResult.delegate = self;
tableViewResult.dataSource = self;
[tableViewResult reloadData];
Everything works as expected except for the header of the top most section, and only on the iPad.
Any ideas what can cause this behavior?
What fixed the issue was changing this function to:
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
int aSection = [[self.sectionsToDisplay objectAtIndex:section] integerValue];
if([[self.groupHeadings objectAtIndex:aSection] isEqualToString:#""])
return nil;
return [self.groupHeadings objectAtIndex:aSection];
}
- (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 am using some background color for the tabelView and style is grouped. The text in the header for the sections is not clear so I need to modify the the text color so that the header text should be visible.
I want to know can we change the header text's color and size?
Adding to terente's answer:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (section == 0) {
CGRect screenRect = [[UIScreen mainScreen] applicationFrame];
UIView* headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenRect.size.width, 44.0)];
//headerView.contentMode = UIViewContentModeScaleToFill;
// Add the label
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, -5.0, 300.0, 90.0)];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.text = #"Header";
headerLabel.textColor = [UIColor blackColor];
headerLabel.highlightedTextColor = [UIColor blackColor];
//this is what you asked
headerLabel.font = [UIFont boldSystemFontOfSize:17];
headerLabel.shadowColor = [UIColor clearColor];
headerLabel.shadowOffset = CGSizeMake(0.0, 1.0);
headerLabel.numberOfLines = 0;
headerLabel.textAlignment = UITextAlignmentCenter;
[headerView addSubview: headerLabel];
[headerLabel release];
// Return the headerView
return headerView;
}
else return nil;
}
You can use [UIFont fontWithName:#"<name of your font>" size:24.0]; for other fonts
Just implement
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
And return your custom view for header.
Edit:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIImageView *headerTitleView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, kSectionHeaderHeight)];
[headerTitleView setImage:sectionHeaderBackgroundImage];
UILabel *sectionTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake((self.view.frame.size.width - 38) / 2, 5, 38, kSectionHeaderHeight - 10)];
sectionTitleLabel.textColor = [UIColor redColor];
sectionTitleLabel.backgroundColor = [UIColor clearColor];
sectionTitleLabel.textAlignment = UITextAlignmentCenter;
sectionTitleLabel.text = #"A";
sectionTitleLabel.font = [UIFont fontWithName:#"yourFont" size:13];
[sectionTitleLabel setAdjustsFontSizeToFitWidth:YES];
[headerTitleView addSubview:sectionTitleLabel];
return headerTitleView;
}
- (void) tableView : (UITableView*) tableView willDisplayHeaderView : (UIView*) view forSection : (NSInteger) section{
[((UITableViewHeaderFooterView) *view).textLabel setFont:(UIFont...)];
}
and you can set the text label from the other table view delegate method.
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
[((UITableViewHeaderFooterView *) view).textLabel setFont:[UIFont fontWithName:#"Your-Font-Name" size:((UITableViewHeaderFooterView *) view).textLabel.font.pointSize]];
}
Notes:
This will set the font to a custom font but keep the pointSize the same.
Also works for willDisplayFooterView.
Don't forget to change Your-Font-Name to your font.
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
if ([view isKindOfClass:[UITableViewHeaderFooterView class]]) {
UITableViewHeaderFooterView *headerView = (UITableViewHeaderFooterView*)view;
[headerView.textLabel setFont:[UIFont fontWithName:#"Gotham Book" size:16.0f]];
}}
We can use header.textlabel object to change other "UILabel" properties for that label.