Countdown timer in UILabel in hidden UITableview section - uitableview

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.

Related

Show header of uitableview on scroll up and hide header of uitableview on scroll down

I am using the following code to add programmatically in a UITableView a header
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(1, 50, 276, 30)];
headerView.backgroundColor = [UIColor colorWithRed:235/255.0f green:235/255.0f blue:235/255.0f alpha:1.0f];
UILabel *labelView = [[UILabel alloc] initWithFrame:CGRectMake(4, 5, 276, 24)];
labelView.text = #"hello";
[headerView addSubview:labelView];
self.tableView.tableHeaderView = headerView;
How do i show the header of the uitableview on scroll up and hide header of uitableview on scroll down. The UITableview is created also programmatically. Any help or suggestion appreciated.
I think this may help
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView.contentOffset.y > self.offsetY) {
self.tableView.tableHeaderView = nil;
} else {
self.tableView.tableHeaderView = self.headerView;
}
self.offsetY = scrollView.contentOffset.y;
}

Unable to create section border in tableview and i want to represent the ui as per the attached image?

how to create section border in tableview and i want to represent the ui as per the attached image ? i displayed spaces between sections but cannot set the border line for section.
For this you need to add and import
#import <QuartzCore/QuartzCore.h>
After that follow below steps
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *section = [[UIView alloc]init];
section.frame = CGRectMake(0, 0, tableview.frame.size.width, height);
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithName:#"imageName"]];
imageView.frame = CGRectMake(0, 0, YOUR_WIDTH, YOUR_HEIGHT); //set your frame
[section addSubview:imageView];
labelSection = [[UILabel alloc]init];
labelSection.textAlignment = NSTextAlignmentLeft;
labelSection.frame = CGRectMake(10, 5, tableview.frame.size.width, 20);
[labelSection setBackgroundColor:[UIColor clearColor]];
[labelSection setFont:[UIFont boldSystemFontOfSize:15]];
NSString *name = #"section title";
labelSection.text = name;
[labelSection setTextColor:[UIColor blackColor]];
[section addSubview:labelSection];
section.layer.borderWidth = 2.0f;
section.layer.cornerRadius = 1.0f;
section.layer.masksToBounds = YES;
section.layer.borderColor=[UIColor lightGrayColor].CGColor;
return section;
}
If you want to set row height
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 44; //It default size.If you want to change to other size you can change.
//OR
// return 90; //Set to your needed size
}

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

tableView viewForHeaderInSection starts with section 1 in ios7

- (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.

custom uitableheader view

I want to put a custom view inside my uitableviewheader, this custom view has an UILabel and a UITextView, the label have a fixed size while uitextview need to change depending the content of uitextview.
For doing it I use this code:
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 100;
}
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,100)];
headerView.backgroundColor = [UIColor blueColor];
NSString *userString = [user username];
UILabel *userLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
[userLabel setTextAlignment:NSTextAlignmentCenter];
[userLabel setText:userString];
userLabel.backgroundColor = [UIColor redColor];
[headerView addSubview:userLabel];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 40, 320, 60)];
NSString *postString = [currentPost objectForKey:#"postTextKey"];
textView.text = postString;
[headerView addSubview:textView];
CGRect frame = textView.frame;
frame.size.height = textView.contentSize.height;
textView.frame = frame;
frame = headerView.frame;
frame.size.height = textView.frame.size.height+40;
headerView.frame = frame;
self.tableView.tableHeaderView.frame = headerView.frame;
return headerView;
}
the problem is that if the headerview have the correct height the tableviewheader is wrong. Where is the mistake?
1) tableHeaderView is different from section header view (headerViewForSection:)
2) If you want to change the height you specify it in the - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section method and then call the reloadData method if the height changes.
Note: you don't want to do that too often.

Resources