Custom UITableView Section not increasing height - ios

I'm trying to make a custom TableView Section. That is higher than the normal section header.
The problem is when i example increase the height from 18 to 30. Nothing happen it does not get any bigger. What am i doing wrong?
i have this code:
-(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:12]];
NSString *string =[[sectionsArray objectAtIndex:section] objectForKey:#"league"];
/* Section header is in 0th index... */
[label setText:string];
[view addSubview:label];
[view setBackgroundColor:[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]]; //your background color...
return view;
}

Implement this method :
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 18.0; // whatever height you want
}
If you are using iOS 7, you can use implemeent the following method also :
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection: (NSInteger)section NS_AVAILABLE_IOS(7_0);

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

UITable view with customise section headers iOS

I have table view , I want to display events according to time. Suppose I have 2 events at 3 PM then I need section header 3 PM and in that two rows with event title.
In Above dummy image There are 2 sections (3:00 PM and 7:00 PM)with respective events. and table header is Date.
I have worked on Table view but without sections. Please help me to achieve this.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return how many section you need
}
//Set your section header height
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 40.0f;
}
//Set your section view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *vw = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tblImageList.frame.size.width, 40)];
[vw setBackgroundColor:[UIColor clearColor]];
UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.tblImageList.frame.size.width, 40)];
[lbl setText:#"your text"];
[lbl setBackgroundColor:[UIColor clearColor]];
[lbl setTextColor:[UIColor colorWithRed:45.0f/255.0f green:206.0/255.0f blue:189.0f/255.0f alpha:1.0f]];
[vw addSubview:lbl];
return vw;
}

How to change index based section common color in UITableView

Normally my screen display is like this:
When I use this code
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
[headerView setBackgroundColor:[UIColor whiteColor]];
return headerView;
}
my output is like this:
My section index text is not visible. I know that the section header view add my custom view. I want to change section header color and also display index text.
#Khawar answer i get this output
this is work 100% and you can change your index or header text color
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
view.tintColor = [UIColor blueColor];
// if you have index/header text in your tableview change your index text color
UITableViewHeaderFooterView *headerIndexText = (UITableViewHeaderFooterView *)view;
[headerIndexText.textLabel setTextColor:[UIColor blackColor]];
}
output:
Try to set the height for UITableView header.
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 30.0;
}
If you only need to change the background color of section header while displaying section title, you don't need to create custom view for that. Custom view would overwrite your default header and your title would not be shown, unless you add custom UILabel in your custom view. Just use following UITableView delegate to change background color.
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
view.tintColor = [UIColor redColor];
}
See results before and after.
----------------Before------------------
----------------After------------------
Hope this fixes the issue.
You have to give title for section... Or you can use this method..
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UILabel *headerView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
[headerView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.5]];
headerView.text = [self tableView:tableView titleForHeaderInSection:section];
return headerView;
}

view in HeaderInSection size not increasing

I have try to give title for section in uitabelview, The label contaning view size is not able to increase more than 50, Here my code,
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(tableView.tag==20)
{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 55)];
[view addSubview:label];
[view setBackgroundColor:[UIColor colorWithRed:166/255.0 green:177/255.0 blue:186/255.0 alpha:1.0]];
return view;
}
}
How to add section like as in the image,
I think this will resolve your issue.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 100;
}
Enjoy the coding......
Go to xib in Table set in setction Height see the below image

Custom table header section

I got an app with group style table.
I tried to customize table header section:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)];
UILabel *userName = [[UILabel alloc] initWithFrame:CGRectMake (0,0,200,30)];
if (section != 0)userName.text = #"lalala";
[userName setFont:[UIFont fontWithName:#"HelveticaNeue" size:20]];
[headerView addSubview:userName];
[headerView setBackgroundColor:[UIColor clearColor]];
[userName setBackgroundColor:[UIColor clearColor]];
return headerView;
}
But my headers close cells:
Why?
You also need to implement tableView:heightForHeaderInSection: to set the correct height for the section header. See the Apple docs
This is happening because the height of your header is set to 0 until you override the other delegate:
– tableView:heightForHeaderInSection:

Resources