tableView viewForHeaderInSection starts with section 1 in ios7 - ios

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
NSString *sectionTitle = #"";
if(section == 0)
sectionTitle = #"Overall Progress";
else
sectionTitle = [[courses objectAtIndex:section-1] objectForKey:#"course-name"];
// Create label with section title
UILabel *label = [[[UILabel alloc] init] autorelease];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
label.frame = CGRectMake(15, 10, 300, 30);
label.font = [UIFont boldSystemFontOfSize:14];
}
else
{
label.frame = CGRectMake(50, 20, 600, 60);
label.font = [UIFont boldSystemFontOfSize:19];
}
label.text = sectionTitle;
label.backgroundColor = [UIColor clearColor];
[label sizeToFit];
// Create header view and add label as a subview
UIView *view;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone){
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 100)];
}
else{
view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 640, 500)];
view.backgroundColor = [UIColor clearColor];
}
[view addSubview:label];
[view autorelease];
return view;
}
Here in this code section returns 1 when I debug and hence the section 0 code doesn't get executed. Thus I am not able to get Overall Progress text in my table header view.

The solution is to implement heightForHeaderInSection with the height that you want.
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 44;
}
from tableView:heightForHeaderInSection:
Prior to iOS 5.0, table views would automatically resize the heights of headers to 0 for sections where tableView:viewForHeaderInSection: returned a nil view. In iOS 5.0 and later, you must return the actual height for each section header in this method.
from tableView:viewForHeaderInSection:
The returned object can be a UILabel or UIImageView object, as well as a custom view. This method only works correctly when tableView:heightForHeaderInSection: is also implemented.
see UITableViewDelegate protocol reference

What is in the implementation of tableView:heightForHeaderInSection:? If you return 0 there for section 0, then our view will be resized to be not visible at all.

Related

Set position of the header at the beginning of the TableView

I have to set my headerView at the beginning of the table View.
It seems to be fixed please help.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0,0,tableView.frame.size.width,40)] autorelease];
headerView.backgroundColor = [Utility consumerLightColor];
UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 0, 250, headerView.frame.size.height)];
headerLabel.text = #"Upcoming Appointments";
headerLabel.textAlignment = NSTextAlignmentLeft;
headerLabel.textColor = [UIColor whiteColor];
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
headerLabel.font = [UIFont fontWithName:#"bauhaus md bt" size:15.0f];
}
else{
headerLabel.font = [UIFont fontWithName:#"bauhaus md bt" size:16.0f];
}
// // NSLog(#"title --> %#",[locationArray objectAtIndex:i]);
headerLabel.backgroundColor = [UIColor clearColor];
[headerView addSubview:headerLabel];
[headerLabel release];
return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if (tableView == appointmentListtableView) {
CGFloat changeheight = 40.00;
return changeheight;
}
else{
return 0.0f;
}
}
All I want is to set the position of the header view (Upcoming Appointments)at the beginning of the table View and it should not be fixed.
Maybe you need to remove table header , not section header ?
To do this you need to set:
tableView.tableHeaderView = nil
See keep always in mind,
If you want to set Static header to table view then its good & best practice to set it as a ,
yourTableView.tableHeader = yourHeaderView;
If your headers are dynamic you don't know weather how much sections want to set, then go with method,
viewForHeaderInSection:tableView
That doesn't mean you can use only one at a time not at all, You can use both at a time also,
Now comes to your case, i think you are doing something wrong in the tableViewHader, set it to nil if you don't need anymore,
But i am curious about one thing even if you have set tableViewHeader still why does it show upper & lower side of your secion header,You are setting tableViewHeader or sectionFooter or showing the UITableViewCell

UITableViewCell scroll over custom UITableView header title

I have a simple UITableViewController which contains about 40 UITableViewCells and the UITableViewController is embedded in a UITabBarController. The UITableViewController is created in Storyboard.
I am overriding the custom UITableViewHeader Text so that I can have a few guides there for users. I am noticing some really weird behaviour with this as represented by the following images:
As can be seen, the UITableView scrolls, but the header text remains there and it looks terrible.
Here is how I'm setting up the custom header:
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// Custom label for the section header titles
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(25, 0, tableView.frame.size.width, 110)];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.numberOfLines = 0;
[label setFont:[UIFont systemFontOfSize:17.0]];
if (section == 0)
{
label.text = #" Add to this by marking a leaflet or video as a favourite.\n\nYou can do this by swiping left on any leaflet or video cell.";
}
return label;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
CGFloat headerHeight = 0;
headerHeight = 110;
return headerHeight;
}
I have worked with different sizes, but no matter what I do, it still behaves in the same way.
If anyone has any thoughts or guidance on this, that would really be appreciated. I just want the header text to also scroll up with the table view and to therefore be hidden from view when scrolling.
You can add your header as the tableHeaderView like this:
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(25, 0, self.tableView.frame.size.width, 110)];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor blackColor];
label.numberOfLines = 0;
[label setFont:[UIFont systemFontOfSize:17.0]];
label.text = #" Add to this by marking a leaflet or video as a favourite.\n\nYou can do this by swiping left on any leaflet or video cell.";
self.tableView.tableHeaderView = label;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
// Custom label for the section header titles
UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 110)];
UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(25, 0, tableView.frame.size.width, 110)];
label.textAlignment = NSTextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.numberOfLines = 0;
[label setFont:[UIFont systemFontOfSize:17.0]];
if (section == 0) {
label.text = #" Add to this by marking a leaflet or video as a favourite.\n\nYou can do this by swiping left on any leaflet or video cell.";
}
[view addSubView:label];
[view setBackgroundColor:[UIColor blueColor]]; // the color for background
return view;}

Countdown timer in UILabel in hidden UITableview section

I have an UIView in each section's header as a subview. That View contains start button and a label with countdown timer.
So when the timer is started and the Tableview scrolls, some sections become hidden than shown again, it re creates my View and resets the timer.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
NSString *arrowName = [arrows objectAtIndex:section];
Workouts *workout = [workoutsArray objectAtIndex:section];
UIView* header;
if (section == 0) {
header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 76)];
} else header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 68)];
header.backgroundColor = [UIColor colorWithRed:0.133f green:0.125f blue:0.247f alpha:1.00f];
//The View with Timer Label
TodayWorkoutView* headerView = [[TodayWorkoutView alloc] initWithFrame:CGRectMake(16, header.frame.size.height-60, header.frame.size.width-32, 60)];
headerView.workout = workout;
[header addSubview:headerView];
return header;
}
How can I make timers in each section, that will work when TableView is scrolling?
I found one solution.
In viewDidLoad method init all Views and add to the new Array:
for (WeekDaysWorkouts *weekWorkout in [self getWokrouts]){
Workouts *fullWorkout = weekWorkout.workouts;
[workoutsArray addObject:fullWorkout];
TodayWorkoutView* headerView = [[TodayWorkoutView alloc] init];
headerView.workout = fullWorkout;
[headerViewsArray addObject:headerView];
}
Then I added Views to the section view:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
NSString *arrowName = [arrows objectAtIndex:section];
UIView* header;
if (section == 0) {
header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 76)];
} else header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 68)];
header.backgroundColor = [UIColor colorWithRed:0.133f green:0.125f blue:0.247f alpha:1.00f];
//The View with Timer Label
TodayWorkoutView* headerView = [headerViewsArray objectAtIndex:section];
headerView.frame = CGRectMake(16, header.frame.size.height-60, header.frame.size.width-32, 60);
[header addSubview:headerView];
return header;}
So, all timers work without interruption, even when scrolling.
If anyone knows better solution, please let me know.

How to correct the tableview section header frame according to the orientation / UITableView width

I have a UITableView with grouped style - and I have a custom header view:
+ (UIView *) viewForHeaderWithText: (NSString*) title
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
UIView *viewHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)];
UILabel *label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.frame = CGRectMake(10.0f, 0.0f, 300.0f, 20.0f);
label.font = [UIFont systemFontOfSize:12.0f];
label.textColor = [UIColor blackColor];
label.textAlignment = NSTextAlignmentLeft;
label.text = title;
if (!IS_OS_7_OR_LATER) {
label.textColor = [UIColor lightGrayColor];
}
[viewHeader addSubview:label];
return viewHeader;
}
}
So the section header width frame is fixed.
but For different orientation and way of presenting the UITableViewController the cell width (iOS6) is changed - as u can see on the screenshots. But I need to make offset for the section title and cell equal.
Now I have:
What I need:
I tried to change the cell width - but not solve this.. Any help?
You need to impelement this below uitableView delegate method:-
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

Add a view under the set header in a section

I want to add a view under the header of a section in a UITableView. Is that possible? Here's my code so far, with that code it replaces the section:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == 4) {
return 10;
} else
{
return 20;
}
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section == 4) {
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 10)];
view.backgroundColor = [UIColor blackColor];
return view;
} else {
return nil;
}
}
Conceptually, I wouldn't think of this as adding another view underneath your header. Instead, consider making your header the height that you want the header to be + the height of the black line. Then, all you have to do is create an additional view (the black line) and add it as a sub view of the header, ex
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 10)];
view.backgroundColor = [UIColor whiteColor];
UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0.0, view.frame.size.height - 2.0, view.frame.size.width, 2.0)];
[lineView setBackgroundColor:[UIColor blackColor]];
[view addSubview:lineView];

Resources