I tried to set this font to the text label. Font is setting correctly.But it is hiding (Please see the screenshot).
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
// Text Color
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
header.textLabel.font = [UIFont fontWithName:#"Damascus" size:13];
}
- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
return #"faq";
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
NSLog(#"Coming here");
return 50;
}
like this
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *tableviewHeader = [[[NSBundle mainBundle] loadNibNamed:#"yourcustomcellname" owner:self options:nil] objectAtIndex:0];
return tableviewHeader;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 50.0;
}
[yourLabel setFont:[UIFont fontWithName: #"Damascus" size: 10.0f]];
[yourSuperView addSubview:yourLabel];
Do like this
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 60)];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, view.frame.size.width-20, 60)];
[label setFont:[UIFont boldSystemFontOfSize:18]];
NSString *string =#"FAQ//....your label text";
[label setText:string];
label.textColor=[UIColor blackColor];
label.numberOfLines=0;
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[view addSubview:label];
[view setBackgroundColor:[UIColor whiteColor]];//set which color you want
return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 60;
}
hope it help's-:)
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;
}
In my table view i am using custom cell
In each section for one row and each section contains header. The section header increase or decrease the height based dynamic text. In that situation, section are jerking when i am scrolling.
# pragma mark - UITableView Delegate methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return auctionResultAry.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (auctionResultAry.count == 0) {
cell = [self tableView:self.tableView EmptycellForRowAtIndexPath:indexPath];
}
else{
cell = [self tableView:self.tableView latestofferscellForRowAtIndexPath:indexPath];
self.tableView.scrollEnabled=YES;
}
return cell;
}
-(UITableViewCell *)tableView:(UITableView *)tableView latestofferscellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellidentifier=#"Cell";
subcell=(CTEAuctionBidNowTVCell *)[tableView dequeueReusableCellWithIdentifier:cellidentifier];
if (subcell==nil)
{
subcell =(CTEAuctionBidNowTVCell *) [[[NSBundle mainBundle] loadNibNamed:#"CTEAuctionBidNowTVCell" owner:self options:nil]objectAtIndex:0];
}
subcell.event_auction_name.text=[NSString stringWithFormat:#"%#",[[auctionResultAry objectAtIndex:indexPath.section ]objectForKey:#"auction_title"]];
subcell.selectionStyle=UITableViewCellSelectionStyleNone;
return subcell;
}
i am writing code in section header
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView;
headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.tableView.frame.size.width, 60)];
headerView.backgroundColor = [UIColor lightGrayColor];
UILabel *auctionIdLbl=[[UILabel alloc] initWithFrame:CGRectMake(headerView.frame.size.width-115, headerView.frame.origin.y+5, 110, 20)];
auctionIdLbl.textColor =[UIColor blackColor];
auctionIdLbl.textAlignment=NSTextAlignmentRight;
auctionIdLbl.font=[UIFont fontWithName:#"Arial-BoldMT" size:13];
auctionIdLbl.text = [NSString stringWithFormat:#"ID: %#",[[auctionResultAry objectAtIndex:section]objectForKey:#"auction_id"]];
[headerView addSubview:auctionIdLbl];
UILabel *titleLnbl = [[UILabel alloc]initWithFrame:CGRectMake(5, headerView.frame.origin.y+5, +headerView.frame.size.width-100, 150)];
titleLnbl.textColor =[UIColor blackColor];
titleLnbl.font=[UIFont fontWithName:#"Arial-BoldMT" size:13];
titleLnbl.numberOfLines=0;
titleLnbl.textAlignment=NSTextAlignmentLeft;
titleLnbl.text = [NSString stringWithFormat:#"%#",[[auctionResultAry objectAtIndex:section]objectForKey:#"auction_title"]];
CGSize size = [self getSizebasedOnText:titleLnbl];
CGFloat titleLnblHght=size.height;
CGRect updateLblHght= titleLnbl.frame;
updateLblHght.size.height=titleLnblHght;
titleLnbl.frame=updateLblHght;
[headerView addSubview:titleLnbl];
return headerView;
}
its due to change in height of row in uitableview.If row height is big jerking is not frequent.
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;
}
}
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.
Hi I have to do menu with submenu in table view. Menu look like:
1. News
---|1.1
---|1.2
---|1.3
2. Weather
3. Ads
4. Cinema
Data to menu is download from server.
I have to do move cell in this menu.
Which framework used?
Look at using the tableView:indentationLevelForRowAtIndexPath: delegate method. Consider using a different table section for each menu section and set the indentation based on indexPath.row > 0 (assuming 2 levels in the menu).
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return self.menus.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 55;;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 70;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[self.menu objectAtIndex:section] objectForKey:#"submenus"];
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 70)];
headerView.backgroundcolor = [UIColor clearcolor];
UILabel *menuTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 2, tableView.frame.size.width - 5, 60)];
menuTitleLabel.backgroundColor = [UIColor clearColor];
menuTitleLabel.textColor = [UIColor colorWithRed:68.0/255.0 green:68.0/255.0 blue:68.0/255.0 alpha:1.0];;
menuTitleLabel.text = [[[self.menus objectAtIndex:section] objectForKey:#"title"] uppercaseString];
menuTitleLabel.font = [UIFont fontWithName:#"Roboto-Bold" size:16.0];
[menuTitleLabel setAdjustsFontSizeToFitWidth:YES];
[headerView addSubview:menuTitleLabel];
return headerView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *submenuArray = [[self.menus objectAtIndex:indexPath.section] objectForKey:#"submenus"];;
static NSString *CellIdentifier = #"MenuCell";
MenuCell *cell = (MenuCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MenuCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.backgroundColor = [UIColor clearColor];
}
cell.title.backgroundColor = [UIColor clearColor];
cell.title.font = [UIFont fontWithName:#"Roboto-Bold" size:16.0];
cell.title.text = [subMenuArray objectAtIndex:indexPath.row]objectForKey:#"title"];
cell.title.textColor = [UIColor colorWithRed:68.0/255.0 green:68.0/255.0 blue:68.0/255.0 alpha:1.0];
return cell;
}