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.
Related
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;
}
I want to hide header and its space if if string/object is null in UITableView. Here is my coding. In this code, if object is null, header space remains there, but it shouldn't.
-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
CompanyObj *compObj=[self.arrCompList objectAtIndex:section];
NSString *compname;
if ([compObj.compName isEqualToString:#""]) {
[self NochangeHeight];
return nil;
} else {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 36)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, tableView.frame.size.width, 36)];
if ([compObj.clientList count] != 0) {
compname = compObj.compName;
[label setFont:[UIFont fontWithName:#"AvenirNextLTPro-Regular" size:25.0f]];
label.textColor = [UIColor blackColor];
label.textAlignment = NSTextAlignmentLeft;
[label setText:compname];
label.backgroundColor = [UIColor orangeColor];
[view setBackgroundColor:[UIColor orangeColor]];
[view addSubview:label];
[self changeHeight];
}
return view;
}
}
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return headerHeight;
}
- (void)changeHeight {
[_clientListGroupTableView beginUpdates];
headerHeight = 50.0;
[_clientListGroupTableView endUpdates];
}
- (void)NochangeHeight {
[_clientListGroupTableView beginUpdates];
headerHeight = 0.0;
[_clientListGroupTableView endUpdates];
}
You have to return a different value in the heightForHeaderInSection: method instead.
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
CompanyObj *compObj=[self.arrCompList objectAtIndex:section];
if ([compObj.compName isEqualToString:#""]) {
return 0;
} else {
return headerHeight;
}
}
You can make the height 0 if it is "", rather than controlling in viewForHeaderInSection.
The method viewForHeaderInSection arrange the view of header according to what you are returning. But it does arrange on to the existing header. So header is always there, and you are just making labels, texts etc. onto that header. As a consequence, when you say it to be "nil", since it is just what should be on the header, programme will understand it as "There is nothing to put on to the header". Thus, header will just be an empty space.
In order not to see header, you should change the height, within the method heightForHeaderInSection. So if you implement also that method as:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
CompanyObj *compObj=[self.arrCompList objectAtIndex:section];
if ([compObj.compName isEqualToString:#""]) {
return 0;
} else {
return headerHeight;
}
}
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;
}
}
UITableView showing separator even there is no rows. But when I use viewForFooterInSection the separator is gone. I want it to keep showing up. What should I do ?
Here is my code when I add footer in it :
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"s"];
cell.textLabel.text = [NSString stringWithFormat:#"%d", indexPath.row + 1];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
return cell;
}
// -------------------------- footer code start here --------------------------
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
view.backgroundColor = [UIColor grayColor];
UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 40)];
title.text = #"footer here";
title.textAlignment = NSTextAlignmentCenter;
title.textColor = [UIColor whiteColor];
[view addSubview:title];
return view;
}
this github project may give you some advices:
CLTableWithFooterViewController
Of course need some change if you would like the footer always display on the screen even if with a lot of cells.
remove - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
and - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
use this code to keep the footer down (but always visible) and keep the separator:
#implementation tableView {
__weak UIView *_staticView;
}
- (void)viewDidLoad {
UIView *staticView = [[UIView alloc] initWithFrame:CGRectMake(0, self.tableView.bounds.size.height-50, self.tableView.bounds.size.width, 50)];
staticView.backgroundColor = [UIColor redColor];
//add other code for UILabel here
[self.tableView addSubview:staticView];
_staticView = staticView;
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 50, 0);
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
_staticView.transform = CGAffineTransformMakeTranslation(0, scrollView.contentOffset.y);
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
// this is needed to prevent cells from being displayed above our static view
[self.tableView bringSubviewToFront:_staticView];
}
I guess you just have a single section,right? Before you using viewForFooterInSection,it just fills blank cells all the way down.But when you call viewForFooterInSection,tableview will put a footer at the end of the section.Since you don't have the second section,it stops draw any cells.So you can make a blank second section,it will fill blank cells to the second section.
I have looked all over the show but cannot find how to simply define my own section title.. So far I have tried this.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 20;
}
from what I have read these are the two delegate you need to add your own title.. so I am woundering what goes in the tableView:viewForHeaderInSection:
I have tried
if (section == 0){
return #"header one";
}
but that didn't cut the mustard.. any help would be appreciated.
If you just want a title, override this method in UITableViewDataSource, rather than the two you have mentioned above.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
Following will help you out.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *av=[[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 22)] autorelease];
NSString * weekdayString = [self getDayFromDate:testDate];
UILabel *lblStr = [[UILabel alloc]initWithFrame:CGRectMake(40, 2, 100, 20)];
lblStr.text = #"header one";
lblStr.backgroundColor = [UIColor clearColor];
lblStr.textColor=[UIColor whiteColor];
lblStr.font = [UIFont boldSystemFontOfSize:15];
[av addSubview:lblStr];
[lblStr release];
}
- (CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section {
return 40;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
Expects you to return a object that is a UIView, such as a UIView itself or something like a UILabel.
So what you could do is return a UILabel which then contains your header. Example:
UILabel *label = [[UILabel alloc] init];
[label setText:#"Section 0"];
[label autorelease];
return label;
Alternatively you could style up a whole view in the interface builder and alloc, init and return that.
If you're looking for just basic text, then gamozzii's answer is what you are looking for.
Simply use this :
//This is the delegate method which you are missing in TableView.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0){
return #"header one";
}
}
whereas if you wish to add the View in the Header or want to do the custom things, then you need to use this delegate method:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;