Im a bit stuck trying to create a custom ios table header. My headers are 30px tall and I am using this code to create them:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
// Create label with section title
UILabel *label = [[UILabel alloc] init] ;
label.frame = CGRectMake(0, 0, 140, 30);
label.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"header_gradient.png"]];
label.textColor = [UIColor whiteColor];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake(0.0, 1.0);
label.font = [UIFont boldSystemFontOfSize:12];
label.text = sectionTitle;
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 140, 30)];
[view addSubview:label];
return view;
}
- (CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section {
return 30;
}
It almost works, but my headers seem to colide with the cells/headers below them:
Can anyone point me in the right direction on cleaning up the headers here?
Thanks in advance
You're missing a [space]:
You have:
- (CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section {
It should be:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
Related
I create multi-column table using multiple TableViews in iOS. It is shown in the attached picture, that is TableViews are located side by side. Since TableView has all functions such as TapGesture for each cell, Scrolling, etc., I like the way I implement.
The only problem is scrolling. Since each TableView has it own scrolling, I have problem to synchronize scrolling among each other. If not, all data in each row at different columns will not match.
How can I synchronize scrolling among TableViews, i.e.if I scroll one TableView, all TableViews should scroll together.
Thanks.
There are some ways to get it to work. But it will be buggy and in my opinion, the incorrect approach to the problem. For this problem, I would use a UICollectionView. That way, you have a single dataSource and much more flexibility than UITableView. You also don't need to be using any hacks to get scrolling synchronised.
Divide your UITableView into the number of columns you want to show as i did in samplecode then add custom labels and provide them tag
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *ci=#"cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ci];
if(cell==nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ci];
}
if (tableView == your tablename)
{
CGFloat widthLabelHeader = CGRectGetWidth(tablename.frame)/7; // 7 is the number of columns in table view
serialnmbrlbl = (UILabel*)[cell.contentView viewWithTag:401];
if(!serialnmbrlbl)
{
serialnmbrlbl = [[UILabel alloc]init];
serialnmbrlbl.frame = CGRectMake(0, 0, widthLabelHeader, 50);
serialnmbrlbl.layer.borderWidth = 0.5;
serialnmbrlbl.tag = 401;
serialnmbrlbl.font = [UIFont fontWithName:#"ArialHebrew-Light" size:14];
[cell.contentView addSubview:serialnmbrlbl];
}
serialnmbrlbl.textAlignment = NSTextAlignmentCenter;
diagnosisnamelbl = (UILabel*)[cell.contentView viewWithTag:402];
if(!diagnosisnamelbl)
{
diagnosisnamelbl = [[UILabel alloc]init];
diagnosisnamelbl.frame = CGRectMake(0, 0, widthLabelHeader, 50);
diagnosisnamelbl.layer.borderWidth = 0.5;
diagnosisnamelbl.tag = 402;
diagnosisnamelbl.font = [UIFont fontWithName:#"ArialHebrew-Light" size:14];
[cell.contentView addSubview:diagnosisnamelbl];
}
diagnosisnamelbl.textAlignment = NSTextAlignmentCenter;
dischargeamtlbl = (UILabel*)[cell.contentView viewWithTag:403];
if(!dischargeamtlbl)
{
dischargeamtlbl = [[UILabel alloc]init];
dischargeamtlbl.frame = CGRectMake(0, 0, widthLabelHeader, 50);
dischargeamtlbl.layer.borderWidth = 0.5;
dischargeamtlbl.tag = 403;
dischargeamtlbl.font = [UIFont fontWithName:#"ArialHebrew-Light" size:14];
[cell.contentView addSubview:dischargeamtlbl];
}
dischargeamtlbl.textAlignment = NSTextAlignmentCenter;
then apply second delegate method
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if(tableView == hospitaltable)
{
UIView *sectionHeaderView = [[UIView alloc] initWithFrame:
CGRectMake(0, 0, hospitaltable.frame.size.width, 45.0)];
sectionHeaderView.backgroundColor = [UIColor colorWithRed:(83/255.f) green:(200/255.f) blue:(36/255.f) alpha:1];
CGFloat widthLabelHeader = CGRectGetWidth(hospitaltable.frame)/7;
UILabel *namelabel1 = (UILabel*)[sectionHeaderView viewWithTag:201];
if(!namelabel1)
{
namelabel1 = [[UILabel alloc]init];
namelabel1.frame = CGRectMake(0, 0, widthLabelHeader, 45);
namelabel1.layer.borderWidth = 0.5;
namelabel1.font = [UIFont fontWithName:#"ArialHebrew-Light" size:14];
[sectionHeaderView addSubview:namelabel1];
}
namelabel1.text = #"Sr. NO.";
namelabel1.textAlignment = NSTextAlignmentCenter;
namelabel1.lineBreakMode = NSLineBreakByTruncatingMiddle;
namelabel1.numberOfLines = 2;
namelabel1.backgroundColor = [UIColor colorWithRed:(83/255.f) green:(200/255.f) blue:(36/255.f) alpha:1];
UILabel *orderdateLbl = (UILabel*)[sectionHeaderView viewWithTag:202];
if (!orderdateLbl) {
orderdateLbl = [[UILabel alloc]init];
orderdateLbl.frame = CGRectMake(CGRectGetMaxX(namelabel1.frame), 0, widthLabelHeader, 45);
orderdateLbl.layer.borderWidth = 0.5;
orderdateLbl.font = [UIFont fontWithName:#"ArialHebrew-Light" size:14];
[sectionHeaderView addSubview:orderdateLbl];
}
orderdateLbl.text = #"DIAGNOSIS NAME";
orderdateLbl.textAlignment = NSTextAlignmentCenter;
orderdateLbl.lineBreakMode = NSLineBreakByTruncatingMiddle;
orderdateLbl.numberOfLines = 2;
orderdateLbl.backgroundColor = [UIColor colorWithRed:(83/255.f) green:(200/255.f) blue:(36/255.f) alpha:1];
apply this delegate method too..
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 45;
}
and lastly
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 45;
}
Take the help of this code you will get the required tableView
I want to display footer text only at last section of the TableView.In TableView I have a lot of TableView section it come from API.But I need to display footer message only at bottom of the tableView section how to do it?
(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 540, 10)];
footer.backgroundColor = [UIColor clearColor];
UILabel *lbl = [[UILabel alloc]initWithFrame:footer.frame];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = #"Your Text";
lbl.textAlignment = NSTextAlignmentCenter;
[footer addSubview:lbl];
return footer;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return 10.0;
}
Currently Im used this code.This shows in all section bottom.I need to display only at bottom of the last tableview section.
try this
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 540, 55)];
if (section == array.count -1){
footer.backgroundColor=[UIColor clearColor];
lbl = [[UILabel alloc]initWithFrame:CGRectMake(0,0,540,40)];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = #" Please add your message";
lbl.textAlignment = NSTextAlignmentCenter;
[lbl setNumberOfLines:10];//set line if you need
[lbl setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:14.0]];//font size and style
[lbl setTextColor:[UIColor blackColor]];
[footer addSubview:lbl];
self.jobsTableView.tableFooterView=footer;
}
return footer;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
if (section == [tableView numberOfSections] - 1) {
return 60.0;
} else {
return 0.0;
}
}
use
if (section == yourArray.count -1)
{
UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 540, 10)];
footer.backgroundColor = [UIColor clearColor];
UILabel *lbl = [[UILabel alloc]initWithFrame:footer.frame];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = #"Your Text";
lbl.textAlignment = NSTextAlignmentCenter;
[footer addSubview:lbl];
}
return footer;
Try this,
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if (section == [tableView numberOfSections] - 1) {
return 10.0;
} else {
return 0.0;
}
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
if (section == [tableView numberOfSections] - 1) {
UIView *footer = ..
....
return footer;
} else {
return nil;
}
}
or you can set view as footer to tableview
UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 540, 10)];
....
[self.tableView setTableFooterView:footer];
Please try to use like this if you wanna footerview of tableview.
UIView *footer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 540, 10)];
footer.backgroundColor = [UIColor clearColor];
UILabel *lbl = [[UILabel alloc]initWithFrame:footer.frame];
lbl.backgroundColor = [UIColor clearColor];
lbl.text = #"Your Text";
lbl.textAlignment = NSTextAlignmentCenter;
[footer addSubview:lbl];
footer.backgroundColor = [UIColor blackColor];
self.tableView.tableFooterView = footer;
I used the following code to set the header view of a uitableview section. The problem is, the background color (mainLightColor_2) doesn't show up until I scroll the tableview. How can I make the background color shown on load?
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 24)];
headerView.backgroundColor = [Utility mainLightColor_2]; //mainLightColor is a blue color
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 2, 200, 20)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [Utility mainDarkColor];
label.text = #"Past Activities";
[headerView addSubview:label];
return headerView;
}
Before scroll:
After scroll:
You should also implement - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section method.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 24;
}
So I found a solution to this. However, I still don't understand why setting the background color of headerView doesn't work.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 24)];
UIView *headerViewBackground = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 24)];
headerViewBackground.backgroundColor = [Utility mainLightColor_2];
[headerView addSubview:headerViewBackground];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 2, 200, 20)];
label.backgroundColor = [UIColor clearColor];
label.textColor = [Utility mainDarkColor];
label.text = #"Past Activities";
[headerView addSubview:label];
return headerView;
}
I created a new Single-view project (using storyboard) in Xcode with a single UITableViewController. Here is the setup code:
- (void)viewDidLoad {
[super viewDidLoad];
_footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
_footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 44, 44)];
l.text = #"Label Label Label Label Label Label Label Label Label";
l.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleWidth;
l.backgroundColor = [UIColor clearColor];
[_footerView addSubview:l];
_footerView.backgroundColor = [UIColor lightGrayColor];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
return _footerView.frame.size.height;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return _footerView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
return [tableView dequeueReusableCellWithIdentifier:#"Cell"];
}
I want the label in the custom table footer view to be drawn at x = 60, but when I run the project, at first the label is invisible (in portrait, screen attached). Then if I rotate once, it becomes visible and if I rotate back to portrait it's visible.
What am I missing?
You seem to be initialising your footer view with a width and height of 44px, yet adding the label outside of its bounds.
Try the following instead:
_footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.tableView.frame), 44)];
_footerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
UILabel *l = [[UILabel alloc] initWithFrame:CGRectInset(_footerView.bounds, 60.0f, 0.0f)];
l.text = #"Label Label Label Label Label Label Label Label Label";
l.autoresizingMask = UIViewAutoresizingFlexibleWidth;
l.backgroundColor = [UIColor clearColor];
[_footerView addSubview:l];
_footerView.backgroundColor = [UIColor lightGrayColor];
As a further point, try not to use [UIColor clearColor] as a label background colour if you can help it - it significantly reduces scrolling performance. In this case you should use [UIColor lightGrayColor] so it matches its superview.
I have three strings that I want to present in header of grouped tableview. Problem is I append this strings in tableView titleForHeaderInSection and I want to present each string in different line with different color and font.
What currently I have is :
What I want is:
I get and append those three string as following
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger) section
{
NSString * presenter = [[self agenda] getBriefingPresenter:section];
NSString * time = [[self agenda] getBriefingTime:section];
NSString * subject = [[[[self agenda] getMeetingBriefings] objectAtIndex:section] objectForKey:#"subject"];
return [NSString stringWithFormat:#"%#\n%# %#", subject, time, presenter ];
}
Method to handle the header font
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString *sectionTitle = [self tableView:tableView titleForHeaderInSection:section];
if (sectionTitle == nil) {
return nil;
}
// Create label with section title
UILabel *label = [[UILabel alloc] init];
label.frame = CGRectMake(45, 0, 484, 23);
label.textColor = [UIColor whiteColor];
label.font = [UIFont fontWithName:#"Century Gothic" size:17];
label.text = sectionTitle;
label.lineBreakMode = UILineBreakModeWordWrap;
label.numberOfLines = 2;
[label sizeToFit];
label.backgroundColor = [UIColor clearColor];
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(100, 300, 320, 400)];
[view addSubview:label];
return view;
}
So how can I parse this sectionTitle and print as three different labels in three lines?
In (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section, create 3 different labels with specific font and colors. You can't set different fonts/colors on a single UILabel;
So your code updated :
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
NSString * presenter = [[self agenda] getBriefingPresenter:section];
NSString * time = [[self agenda] getBriefingTime:section];
NSString * subject = [[[[self agenda] getMeetingBriefings] objectAtIndex:section] objectForKey:#"subject"];
// Create label with section title
UILabel *presenterLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, 0, 484, 23)];
presenterLabel.textColor = presenterColor;
presenterLabel.font = presenterFont;
presenterLabel.text = presenter;
presenterLabel.backgroundColor = [UIColor clearColor];
[presenterLabel sizeToFit];
UILabel *timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, presenterLabel.frame.size.height, 484, 23)];
timeLabel.textColor = timeColor;
timeLabel.font = timeFont;
timeLabel.text = time;
timeLabel.backgroundColor = [UIColor clearColor];
[timeLabel sizeToFit];
UILabel *subjectLabel = [[UILabel alloc] initWithFrame:CGRectMake(45, timeLabel.frame.origin.y + timeLabel.frame.size.height, 484, 23)];
subjectLabel.textColor = subjectColor;
subjectLabel.font = subjectFont;
subjectLabel.text = subject;
subjectLabel.backgroundColor = [UIColor clearColor];
[subjectLabel sizeToFit];
// Create header view and add label as a subview
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 400)];
[view addSubview:presenterLabel];
[view addSubview:timeLabel];
[view addSubview:subjectLabel];
return view;
}
and you don't need the - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection: (NSInteger) section