Create multi-column table using TableViews in iOS - ios

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

Related

How to show different labels in different sections?

I want to set labels in section... How can i set different labels in different section cz in tableview i want to make different sections and i am using the title for header in section.. by doing this i make different sections correctly but not able to differentiate the data between the sections.. Anyone can help me
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
// create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0,-60,300,60)];
// create the label object
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.frame = CGRectMake(0,0,self.view.frame.size.width,60);
UIColor* mainColor = [UIColor colorWithRed:47.0/255 green:168.0/255 blue:228.0/255 alpha:1.0f];
headerLabel.backgroundColor = mainColor;
headerLabel.font = [UIFont boldSystemFontOfSize:18];
headerLabel.textAlignment = UITextAlignmentCenter;
//
// UILabel *firstSection = [[UILabel alloc] initWithFrame:CGRectZero];
// firstSection.frame = CGRectMake(0,0,self.view.frame.size.width,60);
//
// firstSection.font = [UIFont boldSystemFontOfSize:18];
UIView *firstSection = [[UIView alloc] initWithFrame:CGRectMake(0,-60,300,60)];
//headerLabel.textAlignment = UITextAlignmentCenter;
if(section == 0)
headerLabel.text = #"Reciever Party";
UILabel *nameLbl =[[UILabel alloc]initWithFrame:CGRectMake(20, 35, 100 ,50)];
nameLbl.text =#"Name";
nameLbl.textColor=[UIColor blackColor];
UIFont *bold = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
[nameLbl setFont:bold];
nameLbl.backgroundColor =[UIColor clearColor];
[headerLabel addSubview:nameLbl];
UILabel *addLbl =[[UILabel alloc]initWithFrame:CGRectMake(20, 65, 100 ,50)];
addLbl.text =#"Address";
addLbl.textColor=[UIColor blackColor];
UIFont *bold1 = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
[addLbl setFont:bold1];
addLbl.backgroundColor =[UIColor clearColor];
[headerLabel addSubview:addLbl];
if(section == 1)
headerLabel.text = #"Sender Party";
UILabel *senderName =[[UILabel alloc]initWithFrame:CGRectMake(20, 125, 100 ,50)];
senderName.text =#"Address";
senderName.textColor=[UIColor blackColor];
UIFont *bold2 = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
[addLbl setFont:bold2];
senderName.backgroundColor =[UIColor clearColor];
[headerLabel addSubview:senderName];
if(section == 2)
headerLabel.text = #"Third Section Header";
headerLabel.textColor = [UIColor whiteColor];
[customView addSubview:headerLabel];
return customView;
}
I am giving you a sample with hard coded data that how can you show data in section tableView. viewForHeaderInSection is responsible for creating headers of each section, not for showing data in section's row.
I am adding label on cell's content view as you don't want to add this from storyboard/ prototype cell.
Try this:-
In cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"YourIdentifier"];
/*
* If the cell is nil it means no cell was available for reuse and that we should
* create a new one.
*/
if (cell == nil) {
/*
* Actually create a new cell (with an identifier so that it can be dequeued).
*/
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"YourIdentifier"];
UILabel *lbl =[[UILabel alloc]initWithFrame:CGRectMake(20, 15, 100 ,50)];
lbl.textColor=[UIColor blackColor];
UIFont *bold1 = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
[lbl setFont:bold1];
lbl.hidden = NO;
lbl.tag = 101;
lbl.backgroundColor =[UIColor clearColor];
[cell.contentView addSubview:lbl];
}
UILabel *lblCell = (UILabel*)[cell.contentView viewWithTag:101];
if(indexPath.section == 0) //first section
{
if (indexPath.row == 0) //first row of first section
{
lblCell.text =#"Name";
}
if (indexPath.row == 1) //second row of first section
{
lblCell.text =#"Address";
}
}
if(indexPath.section == 1) //second section
{
if (indexPath.row == 0) //first row of second section
{
lblCell.text =#"age";
}
if (indexPath.row == 1) //second row of second section
{
lblCell.text =#"phone no.";
}
}
if(indexPath.section == 2) //third section
{
if (indexPath.row == 0) //first row of third section
{
lblCell.text =#"city";
}
if (indexPath.row == 1) //second row of third section
{
lblCell.text =#"father";
}
}
/* Now that the cell is configured we return it to the table view so that it can display it */
return cell;
}
you have to use like that
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 40;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 225, 40)];
UIImageView *sectionImage = [[UIImageView alloc]initWithFrame:CGRectMake(5, 0, 40, 40)];
sectionImage.image = [UIImage imageNamed:[objMenuImageList objectAtIndex:section]];
sectionImage.contentMode = UIViewContentModeCenter;
[sectionView addSubview:sectionImage];
UILabel *sectionLable = [[UILabel alloc]initWithFrame:CGRectMake(50, 10, 150, 20)];
sectionLable.contentMode = UIViewContentModeLeft;
sectionLable.text = [objMenuList objectAtIndex:section];
[sectionLable setFont:[UIFont systemFontOfSize:14]];
sectionLable.textColor = [UIColor colorWithRed:(206/255.f) green:(180/255.f) blue:(116/255.f) alpha:1.0f];
[sectionView addSubview:sectionLable];
UIButton *sectionButton = [UIButton buttonWithType:UIButtonTypeCustom];
sectionButton.frame = CGRectMake(0, 0, sectionView.frame.size.width, sectionView.frame.size.height -10);
sectionButton.center = sectionView.center;
sectionButton.tag = section;
// sectionButton.backgroundColor = [UIColor yellowColor];
[sectionButton addTarget:self action:#selector(ExpandCell:) forControlEvents:UIControlEventTouchUpInside];
[sectionView addSubview:sectionButton];
sectionView.backgroundColor = [UIColor clearColor];
return sectionView;
}
Just keep It simple And Flexible.
Do it like this..
Step 1. Add one custom cell For Header View with UILabel for title.
Step 2. In cellForRowAtIndexPath , dequeueReusableCellWithIdentifier this cell .
Use condition as...
` if(section==0){
Cell.title.text= #"your title for section 0"
} // here title is UILabel `
By doing this your code is Flexible enough for Any Future Changes!!!

How to Add footer text only in text last section of the 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;

dynamically change height of cells in objective C

I have created an app with a table view. Which uses a view and a label in each cell. But if I create the views and cells in the (!cell) code it returns empty cells and if I remove the (!cell) condition it displays the data but does not take dynamic height. Can anyone please help me.
- (void)viewDidLoad{
NSString *Path = [[NSBundle mainBundle] bundlePath];
NSString *DataPath = [Path stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.plist", LanguageFile]];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] initWithContentsOfFile:DataPath];
self.reloadArray = [tempDict objectForKey:#"Rows"];}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.reloadArray count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// Get data for the current row
NSString *textData = [reloadArray objectAtIndex:indexPath.section]
CGFloat dataTextHeight = [self getLabelHeightForIndex:textData];
if(dataTextHeight < 44)
{
dataTextHeight = 44;
}
return dataTextHeight;
}
-(CGFloat)getLabelHeightForIndex:(NSString *)string
{
CGSize maximumSize = CGSizeMake(280, 10000);
CGSize labelHeightSize = [string sizeWithFont:[UIFont fontWithName:#"Helvetica" size:14.0f] constrainedToSize:maximumSize lineBreakMode:NSLineBreakByWordWrapping];
if(labelHeightSize.height < 44){
labelHeightSize.height = 44;
}
return labelHeightSize.height;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = #"Cell";
static const int textViewTag = 1, textLabelTag = 2;
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"standard_back.png"]];
img.frame = tableView.frame;
tableView.backgroundView = img;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// First view
UIView *textView = [[UIView alloc] initWithFrame: CGRectMake(0.0, 0.0, 280.0, 36.00)];
textView.tag = textViewTag;
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[cell.contentView addSubview:textView];
// First label
UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 0.0, 270.0, 36.00)];
textLabel.tag = textLabelTag;
textLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:14.0f];
textLabel.textColor = [UIColor whiteColor];
textLabel.backgroundColor = [UIColor clearColor];
textLabel.numberOfLines = 0;
textLabel.lineBreakMode = NSLineBreakByWordWrapping;
textLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
// textLabel.clipsToBounds = YES;
[cell.contentView addSubview:textLabel];
}
NSString *textData = [reloadArray objectAtIndex:(indexPath.section)];
CGFloat dataTextHeight = [self getLabelHeightForIndex:textData];
UIView *textView = [cell.contentView viewWithTag:textViewTag];
CGRect textViewFrame = textView.frame;
textView.frame = CGRectMake(0.0, 0.0, textViewFrame.size.width, dataTextHeight);
UILabel *textLabel = [cell.contentView viewWithTag:textLabelTag];
CGRect textLabelFrame = textLabel.frame;
textLabel.frame = CGRectMake(10.0, 0.0, textLabelFrame.size.width, dataTextHeight);
textLabel.text = textData;
textLabel.backgroundColor= [UIColor clearColor];
textLabel.textAlignment = NSTextAlignmentCenter;
cell.backgroundColor = [UIColor colorWithWhite:0 alpha:.65];
cell.textLabel.numberOfLines = 0; // Multiline
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
return cell;
}
Thanks in advance.
I saw a lot of solutions but all was wrong or uncomplet.
You can solve all problems with 5 lines in viewDidLoad and autolayout.
This for objetive C:
_tableView.delegate = self;
_tableView.dataSource = self;
self.tableView.estimatedRowHeight = 80;//the estimatedRowHeight but if is more this autoincremented with autolayout
self.tableView.rowHeight = UITableViewAutomaticDimension;
[self.tableView setNeedsLayout];
[self.tableView layoutIfNeeded];
self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0) ;
For swift 2.0:
self.tableView.estimatedRowHeight = 80
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.setNeedsLayout()
self.tableView.layoutIfNeeded()
self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0)
Now create your cell with xib or into tableview in your Storyboard
With this you no need implement nothing more or override. (Don forget number os lines 0) and the bottom label (constrain) downgrade "Content Hugging Priority -- Vertical to 250"
You can donwload the code in the next url:
https://github.com/jposes22/exampleTableCellCustomHeight
References: http://candycode.io/automatically-resizing-uitableviewcells-with-dynamic-text-height-using-auto-layout/
This is a part of my code which i used in my app. It works for me fine.Ping me if u need help.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
CGSize constraintSize = {230.0, 20000}; //230 is cell width & 20000 is max height for cell
CGSize neededSize = [ [NSString stringWithFormat:#"%#",[cellar objectAtIndex:indexPath.row]] sizeWithFont:[UIFont fontWithName:#"HelveticaNeue-Medium" size:15.0f] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeCharacterWrap];
return MAX(45, neededSize.height +33);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
{ cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
CGSize constraintSize = {230.0, 20000};
UILabel* label = [[UILabel alloc] init];
[label setNumberOfLines:0];
label.backgroundColor = [UIColor clearColor];
[label setFont:[UIFont fontWithName:#"HelveticaNeue-Medium" size:15.0f]];
label.adjustsFontSizeToFitWidth = NO;
CGSize neededSize = [ [NSString stringWithFormat:#"%#",[cellar objectAtIndex:indexPath.row] ] sizeWithFont:[UIFont fontWithName:#"HelveticaNeue-Medium" size:15.0f] constrainedToSize:constraintSize lineBreakMode:UILineBreakModeCharacterWrap];
// NSLog(#"Height%f",neededSize.height);
//NSLog(#"width%f",neededSize.width);
[label setText:[NSString stringWithFormat:#"%#",[cellar objectAtIndex:indexPath.row] ]];
[label setFrame:CGRectMake(10, 2, 230, MAX(neededSize.height+30, 44.0f))];
[[cell contentView] addSubview:label];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
return cell;
}
Hope it helps.!!!
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return "your content height";
}

Separate font and color of words in grouped header UItableview

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

custom header overlaps cells in group

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 {

Resources