how to remove the empty title headers - ios

I am having a table view, in that I am having title headers, they are working well, but the sum of the title header values are getting null, I want to hide that null title header in my table view. how can I do this process. please help me in coding.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [reverseOrder1 objectAtIndex:section];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 60;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 75)];
UILabel *headerTitile =[[UILabel alloc]initWithFrame:CGRectMake(20, -5, tableView.bounds.size.width-20, 75)];
headerTitile.text = [reverseOrder1 objectAtIndex:section];
headerTitile.textColor = [UIColor whiteColor];
headerTitile.TextAlignment=NSTextAlignmentCenter;
[headerView addSubview:headerTitile];
headerTitile.font = [UIFont fontWithName:#"Arial" size:16.0f];
headerView.backgroundColor = [UIColor colorWithRed:48/255.0f green:119/255.0f blue:21/255.0f alpha:1];
return headerView;
}
I am getting like above image,i ant to hide the null value title headers.

Make changes in heightForHeaderInSection is return 0 if no objects
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
//Note use your main array which defines section's row and according to each section it may be dictionary or array use accordingly
NSArray *arrObjects = yourSectionsRowData[section];
if([arrObjects count]>0])
return 60.0;
else
return 0.0;
}
Also return view nil if not found any objects in viewForHeaderInSection
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//Note use your main array which defines section's row and according to each section it may be dictionary or array use accordingly
NSArray *arrObjects = yourSectionsRowData[section];
if([arrObjects count]>0])
return nil;
else
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 75)];
UILabel *headerTitile =[[UILabel alloc]initWithFrame:CGRectMake(20, -5, tableView.bounds.size.width-20, 75)];
headerTitile.text = [reverseOrder1 objectAtIndex:section];
headerTitile.textColor = [UIColor whiteColor];
headerTitile.TextAlignment=NSTextAlignmentCenter;
[headerView addSubview:headerTitile];
headerTitile.font = [UIFont fontWithName:#"Arial" size:16.0f];
headerView.backgroundColor = [UIColor colorWithRed:48/255.0f green:119/255.0f blue:21/255.0f alpha:1];
return headerView;
}
}

Related

viewForHeaderInSection stopped working in xcode 8

I have a tableView with 2 sections, the sections array is fixed with 2 elements. The headers for the sections showed fine as code shows below in Xcode 7. I just upgraded to Xcode 8, and the section headers don't show anymore, the code below doesn't get called anymore.
Any ideas?
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
/* Create custom view to display section header... */
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 18)];
[label setFont:[UIFont boldSystemFontOfSize:16]];
NSString *date =[sections objectAtIndex:section];
/* Section header is in 0th index... */
[label setText:date];
[view addSubview:label];
[view setBackgroundColor:[DeviceHelper Orange]]; //[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]]; //your background color...
return view;
}
Use this method as it is, you will see the header
NB : set the delegate and datasource of tableView
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 100;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
static NSString *reuseId = #"reuseid";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseId];
}
cell.textLabel.text = #"Test";
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 18)];
/* Create custom view to display section header... */
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 18)];
[label setFont:[UIFont boldSystemFontOfSize:16]];
NSString *date = #"22nd Sept 2016";
/* Section header is in 0th index... */
[label setText:date];
[view addSubview:label];
[view setBackgroundColor:[UIColor orangeColor]]; //[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/25
return view;
}
Make sure you have heightForHeaderInSection in addition to your viewForHeaderInSection function
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 30;
}

How to remove the empty section header in UITableView

I am having a UITableView in that I am having three title headers.problem is: when my title header value become empty means that header should not show in the table view. I have tried many method its not helped for me. can anyone help me please.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [reverseOrder1 objectAtIndex:section];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 60;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 75)];
UILabel *headerTitile =[[UILabel alloc]initWithFrame:CGRectMake(20, -5, tableView.bounds.size.width-20, 75)];
headerTitile.text = [reverseOrder1 objectAtIndex:section];
headerTitile.textColor = [UIColor whiteColor];
headerTitile.TextAlignment=NSTextAlignmentCenter;
[headerView addSubview:headerTitile];
headerTitile.font = [UIFont fontWithName:#"Arial" size:16.0f];
headerView.backgroundColor = [UIColor colorWithRed:48/255.0f green:119/255.0f blue:21/255.0f alpha:1];
return headerView;
}
You can use like this
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if ([tableView.dataSource tableView:tableView numberOfRowsInSection:section == 0])
{
return 0;
}
else
{
return 60;
}
}
In addition to Ravi's answer, make sure you set the section height property of table view to 0 in storyboard.

How to change font style and background color in titleForHeaderInSection method

after long reading and checking code, I am proud to have a custom table view with sections and section titles, all populated from core data objects. Now I would need to customize the section title and background color. I have seen it done but in a viewForHeaderInSection method. Is is not possible inside my titleForHeaderInSection method?
Here you have my method:
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
id <NSFetchedResultsSectionInfo> theSection = [[self.fetchedResultsController sections]objectAtIndex:section];
NSString *sectionname = [theSection name];
if ([sectionname isEqualToString:#"text 1"]){
return #"Today";
}
else if ([sectionname isEqualToString:#"text 2"]){
return #"Tomorrow";
}
if ([[self.fetchedResultsController sections]count]>0){
id<NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections]objectAtIndex:section];
return [sectionInfo name];
}
else{
return nil;
}
}
simple and optimized
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
// Background color
view.tintColor = [UIColor whiteColor];//[UIColor colorWithRed:77.0/255.0 green:162.0/255.0 blue:217.0/255.0 alpha:1.0];
// Text Color
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
[header.textLabel setTextColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"tableSection"]]];
}
Here's an example that uses your existing code to set the title text, but lets you use UITableViewHeaderFooterView to adjust the appearance:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
static NSString *header = #"customHeader";
UITableViewHeaderFooterView *vHeader;
vHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:header];
if (!vHeader) {
vHeader = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:header];
vHeader.textLabel.backgroundColor = [UIColor redColor];
}
vHeader.textLabel.text = [self tableView:tableView titleForHeaderInSection:section];
return vHeader;
}
If you want, you can even subclass UITableViewHeaderFooterView just like you'd subclass UITableViewCell to customize the appearance even further.
Swift implementation of #codercat's answer:
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
view.tintColor = UIColor(red: 0.967, green: 0.985, blue: 0.998, alpha: 1) // this example is a light blue, but of course it also works with UIColor.lightGray
var header : UITableViewHeaderFooterView = view as UITableViewHeaderFooterView
header.textLabel.textColor = .red
}
Make a Custom HeaderView by :
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *viewHeader = [UIView.alloc initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 28)];
UILabel *lblTitle =[UILabel.alloc initWithFrame:CGRectMake(6, 3, 136, 21)];
[lblTitle setFont:[UIFont fontWithName:#"HelveticaNeue" size:13]]; //Font style
[lblTitle setTextColor:[UIColor blackColor]];
[lblTitle setTextAlignment:NSTextAlignmentLeft];
[lblTitle setBackgroundColor:[UIColor clearColor]]; //Background
[viewHeader addSubview:lblTitle];
return viewHeader;
}
Give this label any text; I suggest make a seperate NSArray for header titles.
I think that in titleForHeaderInSection you can't change section view properties.
Add to your project viewForHeaderInSection and then add custom custom view with label on it, e.g.
see this hint:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 0)] autorelease];
UILabel *label = [[[UILabel alloc] init] autorelease];
label.frame = CGRectMake(0, 0,300, TABLE_HEADER_HEIGHT);
label.backgroundColor = [UIColor clearColor];
label.textColor = TABLE_HEADER_TITLE_COLOR;
label.font = [UIFont fontWithName:FONT_NAME size:14.f];
label.text = [self tableView:tableView titleForHeaderInSection:section];
if (section == 3) {
[headerView setBackgroundColor:[UIColor whiteColor]];
} else {
[headerView setBackgroundColor:[UIColor redColor]];
}
[headerView addSubview:label];
return headerView;
}
You can set title for label according section number.

limit the width of view for header and view for footer of a UItableView

In the screenshot below, the blue colored line is the header and footer of the sections of my tableView (In the tableView, I am treating rows as sections).
However, i want the blue line to be just below the row of the tableView (of same width as the row). Any idea how to do it??
-(UIView*)tableView:(UITableView*)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 333, 1)] ;
headerView.backgroundColor = [UIColor blueColor];
return headerView;
}
-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 333, 1)] ;
footerView.backgroundColor = [UIColor blueColor];
return footerView;
}
You can add UIView with Blue color above the UIView for which set the background color to Clear Color
-(UIView*)tableView:(UITableView*)tableView viewForFooterInSection:(NSInteger)section
{
UIView *dummyfooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 2)] ;
dummyfooterView.backgroundColor = [UIColor clearColor];
// Widht should be less than dummyfooterView
UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 320-20, 2)] ;
footerView.backgroundColor = [UIColor blueColor];
[dummyfooterView addSubview:footerView];
return dummyfooterView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 2;
}
I am not sure though, hope this may help you! The output will be like below screen. I have used Grouped tableview style here.
I think the best way is to add 1px view at bottom of cell at row at indexpath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MessageBoxCell";
LGMessageBoxCell *cell = (LGMessageBoxCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, cell.fram.size.height - 1, cell.fram.size.width, 1)] ;
headerView.backgroundColor = [UIColor blueColor];
cell.backgroundView = headerView;
return cell;
}
Set height for header and fooder
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 58;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 58;
}

custom header overlaps cells in group

Im a bit stuck trying to create a custom ios table header. My headers are 30px tall and I am using this code to create them:
- (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] init] ;
label.frame = CGRectMake(0, 0, 140, 30);
label.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"header_gradient.png"]];
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.font = [UIFont boldSystemFontOfSize:12];
label.text = sectionTitle;
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 140, 30)];
[view addSubview:label];
return view;
}
- (CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section {
return 30;
}
It almost works, but my headers seem to colide with the cells/headers below them:
Can anyone point me in the right direction on cleaning up the headers here?
Thanks in advance
You're missing a [space]:
You have:
- (CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section {
It should be:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

Resources