ViewForHeaderInSection code works only on header dequeueing - ios

I have custom tableView header, but the code where I specify header text color and separator line works only when the headers are dequeued, when I scroll them off the screen. Only then they return with white text color and visible separator. What is wrong with my code that the headers do not return in the correct state right on the first load?
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return #"FIRST HEADER";
} else if (section == 1) {
return #"SECOND HEADER";
} else {
return #"THIRD HEADER";
}
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UITableViewHeaderFooterView *myHeaderView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:#"header"];
if (!myHeaderView) {
myHeaderView = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:#"header"];
}
myHeaderView.textLabel.textColor = [UIColor whiteColor];
CGFloat leftMargin = 0.0;
CGFloat lineWidth = 1.0;
UIView *separatorLine = [[UIView alloc] initWithFrame:CGRectMake(leftMargin, myHeaderView.frame.size.height - lineWidth, myHeaderView.frame.size.width - leftMargin, lineWidth)];
separatorLine.backgroundColor = [UIColor whiteColor];
[myHeaderView addSubview:separatorLine];
return myHeaderView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 20.0;
}
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
if ([view isMemberOfClass:[UITableViewHeaderFooterView class]]) {
((UITableViewHeaderFooterView *)view).backgroundView.backgroundColor = [UIColor clearColor];
}
}

The reason is that your header's frame is equal to CGRectZero before dequeueing. It means, that header will be resized in future and you need to set appropriate autoresizing mask to separator. Then, separator will also be resized.
UIView *separatorLine = [[UIView alloc] initWithFrame:CGRectMake(leftMargin, 0, 0, lineWidth)];
separatorLine.backgroundColor = [UIColor redColor];
separatorLine.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth;
[header.contentView addSubview:separatorLine];

Related

How to hide header if string/object is null in UITableView

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

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.

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

UITableView separator gone when using viewForFooterInSection

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.

Setting header for only one uitableview

I need help. I have three UITableViews in one UIView. However, I only want one UITableView to have a header. How can I say it in code? I already have the following code. Thanks guys.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (tableView == Table1) {
return #"My Header Text";
} else if (tableView == Table2) {
return nil;
} else if (tableView == Table3) {
return nil;
}
}
The code you provided will be add a header view for a certain section only. So If you have two or more section then you need to write extra logic to set the header view for each section.
Here it is an easy way to set the tableHeaderView for single table is.
UILabel *label = [[UILabel alloc]initWithFrame:CGRectZero];
label.text = #"my table header";
table1.tableHeaderView = label;
If that's not working you could also try to return zeroes for header's heights as well:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (tableView == Table1)
{
return 50.0; // or what's the height?
}
else
{
return 0.0;
}
}
Try this
- (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.textColor = [UIColor blackColor];
label.font = [UIFont boldSystemFontOfSize:16];
label.text = sectionTitle;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, SectionHeaderHeight)];
[view addSubview:label];
return view;
}

Resources