UItableView: Nested Header sections - ios

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.

Related

TableView Seperator line gets added to the section header view, what to do?

I got a table view with two sections, no crazy code, just my delegate methods.
It works pretty fine, like i want it to work. It should just look like on this screenshot:
Now the problem is: Sometimes while scrolling or flicking the scoll view to the bounds, this happens (if you can't see it: There is 1 or 1/2 pixel in gray on the top of the second section header, what is not intended to be so):
So, is this a iOS 7.1 or 7.x bug? I'm not using a custom view for the header. Does anyone know how to fix this?
Feedback really is appreciated.
I had this same problem that I battled for a few weeks, and the way I solved it was to set the tableView's separatorStyle to UITableViewCellSeparatorStyleNone, and add a custom subview that is a line to the cell's contentView.
Then in your cellForRowAtIndexPath method, hide the line subview of the last cell in the section:
- (UIView *)lineView
{
// Your frame will vary.
UIView *colorLineView = [[UIView alloc]initWithFrame:CGRectMake(82, 67.5, 238, 0.5)];
colorLineView.backgroundColor = [UIColor blackColor];
return colorLineView;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
static NSString *identifier = #"cellIdentifier";
UIView *lineView = [self lineView];
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
cell.selectionStyle = UITableViewCellSelectionStyleDefault;
[cell.contentView addSubview:lineView];
}
if (indexPath.section == 0)
{
if (indexPath.row == keys.count -1)
{
lineView.hidden = YES;
}
}
return cell;
}
It may be recycling one of the cell views with the separator from the scroll. This is a long shot, but what if you were to try tweaking the footer view for the section by returning an empty view?
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
return [[UIView alloc] init];
}
It's also a good trick for removing empty cells from the table when you have only a couple rows.
I tried it with multiple different things and the cleanest approach i found is this.
I created a custom view for the header, but wanted it to look the same as the original not modified header:
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 34)];
[headerView setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, tableView.frame.size.width, 34)];
[label setFont:[UIFont boldSystemFontOfSize:14]];
if (section == 0) {
NSMutableArray *difficultyArray = [dictionary objectForKey:#"Difficulty"];
NSString *difficulty = [difficultyArray objectAtIndex:0];
[label setText:[NSString stringWithFormat:#"Time Challenge (%#)", difficulty]];
} else {
[label setText:#"Freeplay (5x5 board)"];
}
[headerView addSubview:label];
return headerView;
}
Now we got the sections as they would appear without custom header views, but the bug still exists. I made it simple and clean:
UIView *lineFix = [[UIView alloc] initWithFrame:CGRectMake(0, 77.5, self.tableView.frame.size.width, 0.5)];
lineFix.backgroundColor = [UIColor groupTableViewBackgroundColor];
[self.tableView addSubview:lineFix];
Now we set a view over the buggy seperator with a height of 0.5 pixel, the seperator isn't visible anymore. Between the two section headers now is a 0.5 height view what shouldn't be there, but since i set it the same color as the section background color it isn't noticeable. The view moves, because it is a subview of the tableview, the same direction like the tableview.
If you have questions, just add a comment.

Custom UITableView Section not increasing height

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

Unable to view custom UITableViewCell in iOS

I trying to display results in a UITableView in my iOS application using a custom UITableViewCell. I am able to view the contents of my Array if I use the cell's textLabel, and detailTextLabel properties, but if I try to link the UITableView with the custom cell class to display the contents of the Array, I only get the default values from Interface Builder displaying in my table.
This is the relevant code that I have:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomTableCell *cell = (CustomTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
TransactionList *tList = [_list objectAtIndex:indexPath.row];
/*
cell.transaction.text = tList.transaction;
cell.type.text = tList.type;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"ddMMYYYY"];
NSString *dateAsString = [formatter stringFromDate:tList.date];
cell.date.text = dateAsString;
*/
//this code below displays the contents just fine, but the block of code above does not.
cell.textLabel.text = tList.transaction;
cell.detailTextLabel.text = [NSString stringWithFormat:#"%#, %#", tList.type, tList.status];
return cell;
}
The method below is what I have in my CustomTableCell class:
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.selectionStyle = UITableViewCellSelectionStyleNone;
self.contentView.frame = CGRectMake(0, 0, 320, 80);
_transaction = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 80, 40)];
[_transaction setBackgroundColor:[UIColor clearColor]];
[_transaction setFont:[UIFont systemFontOfSize:12]];
[_transaction setNumberOfLines:2];
[_transaction setLineBreakMode:NO];
[self.contentView addSubview:_transaction];
_date = [[UILabel alloc] initWithFrame:CGRectMake(100, 5, 80, 40)];
[_date setBackgroundColor:[UIColor clearColor]];
[_date setFont:[UIFont systemFontOfSize:12]];
[_date setNumberOfLines:2];
[_date setLineBreakMode:NO];
[self.contentView addSubview:_date];
_type = [[UILabel alloc] initWithFrame:CGRectMake(200, 5, 80, 40)];
[_type setBackgroundColor:[UIColor clearColor]];
[_type setFont:[UIFont systemFontOfSize:12]];
[_type setNumberOfLines:2];
[_type setLineBreakMode:NO];
_type.lineBreakMode = NO;
[self.contentView addSubview:_type];
}
return self;
}
Where _transaction, _date, and _type are labels. Here is a screenshot of my Interface Builder inside MainStoryboard:
I have tried changing the style from "Basic", to "Custom", to "Subtitle", but that does not help. It only displays the default values of the table, and those settings change the way they appear, but at no point are the contents of my Array showing up in the table. Can anyone see what it is I am doing wrong? I really think the problem is something simple, but unfortunately I can't put my finger on it.
Thanks in advance to all who reply.
You don't show the 3 objects in the Master View cell. Change the 'Style' from Basic to Custom and drop in your objects.
In the 'Identity Inspector' make sure to rename the Class to 'CustomTableCell'.
Do the views (transaction, date & type) have properties in the CustomTableCell.h and are they connected to the 'CustomTableCell' in IB?
Here's an image to point out the last two:

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:

iOS - Custom section header bounds issue

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

Resources