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:
Related
Here, I had set description, price as well as location from-to, but not able to set the date and time as shown in figure above. Format facing some issue to set the date in one header and time in another accordingly. If any link which shows how to set nested headers would help me a lot.
Thanks in advance.
Yes, as shown in figure not able to add a separator in between.I also tried this
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
NSMutableArray *arrdate=[[NSMutableArray alloc]init];
arrdate=[date1 objectAtIndex:section] ;
NSLog(#"%#",arrdate);
NSMutableArray *arrtime=[[NSMutableArray alloc]init];
arrtime=[time objectAtIndex:section];
NSString *strvalue=[arrdate objectAtIndex:0];
UIView *view;
if([strvalue isEqualToString: strcheck])
{
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 15)];
/* Create custom view to display section header... */
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 15)];
[label1 setFont:[UIFont boldSystemFontOfSize:12]];
strcheck=[arrdate objectAtIndex:0] ;
NSString *string =[arrtime objectAtIndex:0];
/* Section header is in 0th index... */
[label1 setText:string];
[view addSubview:label1];
}
else{
/* Create custom view to display section header... */
view= [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 100)];
//main time
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, tableView.frame.size.width, 15)];
[label setFont:[UIFont boldSystemFontOfSize:12]];
strcheck=[arrdate objectAtIndex:0] ;
/* Section header is in 0th index... */
[label setText:strcheck];
[view addSubview:label];
//time..
UILabel *labeltime = [[UILabel alloc] initWithFrame:CGRectMake(10,20, tableView.frame.size.width, 15)];
[labeltime setFont:[UIFont boldSystemFontOfSize:12]];
NSString *stringtime =[arrtime objectAtIndex:0];
/* Section header is in 0th index... */
[labeltime setText:stringtime];
[view addSubview:labeltime];
}
return view;
}
but facing problem that date view is getting disturb while scrolling tableview.
Create a xib file which has a UIView and labels inside that as shown in the attached screenshot.
Have a source files named CustomHeader which is subclass of UIView.
Map your xib view with CustomHeader class.
Generate IBOutlets for the labels added in xib.
In your ViewController viewForHeaderInSection method,
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSArray * bundleArray = [[NSBundle mainBundle] loadNibNamed:#"View" owner:self options:nil];
CustomUIClass * view = (CustomUIClass *) [bundleArray objectAtIndex:0];
return view;
}
Also override heightForHeaderInSection
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 69.0f;
}
here '69' is the height of the xib. Now your tableview looks like as attached screenshot.
Like this you can customise your header as per your requirement.
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;
}
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);
UITableView's section headers are less identifiable in iOS 7. Are there any way to set the UITableView section header color in iOS 7 directly rather creating a new UIView.
Note: I found some solutions by creating a new UIView in,
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
but I really wanted to keep the Apple's properties except color. Are there any way to do it without this method.
Implement tableView:willDisplayHeaderView:forSection: and update the view that you are passed.
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
{
UITableViewHeaderFooterView *v = (UITableViewHeaderFooterView *)view;
v.backgroundView.backgroundColor = [UIColor darkGrayColor];
}
(assuming that you supplied a UITableViewHeaderFooterView instance in your implementation for tableView:viewForHeaderInSection:)
In the case of Swift 3 the way is to achieve it via UITableViewDelegate method:
override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
if let headerView = view as? UITableViewHeaderFooterView {
headerView.backgroundColor = UIColor.gray
}
}
With this method you can set font size, font style and Header background also.
There are have 2 method for this:
First Method
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{
UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
header.backgroundView.backgroundColor = [UIColor darkGrayColor];
header.textLabel.font=[UIFont fontWithName:#"Open Sans-Regular" size:12];
[header.textLabel setTextColor:[UIColor whiteColor]];
}
Second Method
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UILabel *myLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 30)];
// myLabel.frame = CGRectMake(20, 8, 320, 20);
myLabel.font = [UIFont fontWithName:#"Open Sans-Regular" size:12];
myLabel.text = [NSString stringWithFormat:#" %#",[self tableView:FilterSearchTable titleForHeaderInSection:section]];
myLabel.backgroundColor=[UIColor blueColor];
myLabel.textColor=[UIColor whiteColor];
UIView *headerView = [[UIView alloc] init];
[headerView addSubview:myLabel];
return headerView;
}
I'm trying to create my own section header for my grouped cells. When I create it, using the following code, it looks as if the bounds are off. More specifically the height of the CGRect
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *header = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 50)] autorelease];
[header setBackgroundColor:[UIColor clearColor]];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(10, 3, tableView.bounds.size.width, 18)] autorelease];
label.textColor = [UIColor redColor];
label.text = #"SOME TEXT";
label.backgroundColor = [UIColor clearColor];
[header addSubview:label];
return header;
}
Any idea why the height is off? Am I missing something?
Have you also implemented tableView:heightForHeaderInSection: as described in the docs? This should return 50 for your table.
Just curious as to why you aren't defining the TableView type (Grouped), setting the properties of the section headers in IB and then using:
- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section