I want to set multiple running timer value in tableview so user easily checked how many time is remaining and stop any specific timer.
Multiple Timer Start and updated value display in NSLog perfectly.
User set NSTimer for particular recipe.
Now suppose user set timer for recipe1 in recipedetail and now back from that recipedetail.
Then again set timer for recipe2 in recipedetail and back from that recipedetail.
and i want to All timer value display in UITableView. And tableview placed on recipedetail screen.
So my point is TimerUpdated value display in NSLog but not display in tableview because every Recipedetails screen generate new object of UITableView So value is not updated perfactly
-(IBAction)okBtn:(id)sender
{
[self countdownTimer];
}
-(void)countdownTimer {
[[NSUserDefaults standardUserDefaults]setObject:recipeboxId forKey:[NSString stringWithFormat:#"timer_%#",recipeboxId]];
[[NSUserDefaults standardUserDefaults]synchronize];
// set timer
[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
(theAppDelegate).timer = [NSTimer scheduledTimerWithTimeInterval:0.6f
target:self
selector:#selector(updateCounter:)
userInfo:recipeboxId
repeats:YES];
[(theAppDelegate).timer fire];
// [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];//Timer run in background
[self CreateLocalNotification];
}
-(void)calculateTimeFromPicker
{
NSString *hoursStr = [NSString stringWithFormat:#"%#",[hoursArray objectAtIndex:[self.picker_timer selectedRowInComponent:0]]];
NSString *minsStr = [NSString stringWithFormat:#"%#",[minsArray objectAtIndex:[self.picker_timer selectedRowInComponent:1]]];
NSString *secsStr = [NSString stringWithFormat:#"%#",[secsArray objectAtIndex:[self.picker_timer selectedRowInComponent:2]]];
int hoursInt = [hoursStr intValue];
int minsInt = [minsStr intValue];
int secsInt = [secsStr intValue];
interval = secsInt + (minsInt*60) + (hoursInt*3600);
secondsLeft=interval;
}
- (void)updateCounter:(NSTimer *)theTimer {
if(secondsLeft > 0 )
{
secondsLeft -- ;
hours = secondsLeft / 3600;
minutes = (secondsLeft % 3600) / 60;
seconds = (secondsLeft %3600) % 60;
[[NSUserDefaults standardUserDefaults]setInteger:secondsLeft forKey:[NSString stringWithFormat:#"secondsLeft_%#",recipeboxId]];
[[NSUserDefaults standardUserDefaults]synchronize];
NSLog(#"timer :%#",[NSString stringWithFormat:#"%02d:%02d:%02d", hours, minutes, seconds]);
NSString *timer_updated_value=[NSString stringWithFormat:#"%02d:%02d:%02d", hours, minutes, seconds];
[[NSUserDefaults standardUserDefaults]setObject:timer_updated_value forKey:[NSString stringWithFormat:#"updated_timer_%#",[(theAppDelegate).timer userInfo]]];
[[NSUserDefaults standardUserDefaults]synchronize];
recipeArr = [[(theAppDelegate).Timer_recipeIdArr reverseObjectEnumerator] allObjects];
for (int section = 0; section < [recipeArr count]; section++)
for (int section = 0; section < [recipeArr count]; section++)
{
for (int row = 0; row < (int)[self.timerWindowTbl numberOfRowsInSection:section]; row++)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
UITableViewCell *cell = [self.timerWindowTbl cellForRowAtIndexPath:indexPath];
for(UILabel *lbl in [cell.contentView subviews])
{
if([lbl isKindOfClass:[UILabel class]])
{
if(lbl.tag == 1)
{
NSString *timer_desc= [[NSUserDefaults standardUserDefaults]objectForKey:[NSString stringWithFormat:#"timerDescString_%#",recipeArr[indexPath.row]]];
NSString *recipe_title=[[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:#"timerRecipeTitle_%#",recipeArr[indexPath.row]]];
NSString *updated_timer=[[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:#"updated_timer_%#",recipeArr[indexPath.row]]];
NSString *desc=[NSString stringWithFormat:#"%#\n'%#\' %# %#.",recipe_title,timer_desc,NSLocalizedString(#"is done in",nil),updated_timer];
lbl.text=updated_timer;
[lbl setNeedsDisplay];
NSLog(#"*******************************lbl.text:%#",lbl.text);
lbl.numberOfLines=0;
[lbl sizeToFit];
for(UIButton *btn in [cell.contentView subviews])
{
if([btn isKindOfClass:[UIButton class]])
{
if(btn.tag==2)
{
btn.titleLabel.text=[NSString stringWithFormat:#"%#",recipeArr[indexPath.row]];
}
}
}
break;
}
}
}
}
}
}
else
{
secondsLeft = hours = minutes = seconds = 0;
[self TimerInvalidate];
NSLog(#"Time out :");
}
}
i think u need to refractor your code, u need reload the tableview cell each time when recipe timer stared for particular recipe
this is sample demo u can try with separate project,
first create a Recipe class subclass of NSObject like below,
in Recipe .h file
#import <Foundation/Foundation.h>
#class Recipe;
#protocol RecipeDelegate <NSObject> //we need to notify which recipe time changing
- (void)timerChangedInRecipe:(Recipe *)recipe;
#end
#interface Recipe : NSObject
#property (nonatomic, assign) NSInteger recipeboxId;
#property (nonatomic, strong) NSString *recipeName;
#property (nonatomic, strong) NSString *countTimer;
#property (nonatomic, assign) NSTimeInterval secondsLeft;
#property (nonatomic, assign) NSTimeInterval interval;
#property (nonatomic, assign) int hours;
#property (nonatomic, assign) int minutes;
#property (nonatomic, assign) int seconds;
#property (nonatomic, strong) NSTimer *timer;
#property (nonatomic, assign) id<RecipeDelegate>delegate;
- (id)initWithRecipieId:(NSInteger)recipeId recipeName:(NSString *)name;
- (void )currentTimerString;
- (void)startTimer; //use to start the timer
- (void)stopTimer; //stop the timer
#end
in Recipe.m file
#import "Recipe.h"
#implementation Recipe
- (id)init
{
self = [super init];
if(self)
{
}
return self;
}
- (id)initWithRecipieId:(NSInteger)recipeId recipeName:(NSString *)name
{
self = [super init];
if(self)
{
self.recipeboxId = recipeId;
self.recipeName = name;
}
return self;
}
- (void)currentTimerString //this is similar to your method updateCounter:
{
self.secondsLeft -- ;
if(self.secondsLeft > 0 )
{
_hours = (int)self.secondsLeft / 3600;
_minutes = ((int)self.secondsLeft % 3600) / 60;
_seconds = ((int)self.secondsLeft %3600) % 60;
self.countTimer = [NSString stringWithFormat:#"%02d:%02d:%02d", self.hours, self.minutes, self.seconds];
if([self.delegate respondsToSelector:#selector(timerChangedInRecipe:)])
[self.delegate timerChangedInRecipe:self]; //notifying the tableview to reload the particular cell
}
else
{
_hours = _minutes = _seconds = 0;
[self stopTimer]; //timer finished stop the timer
}
}
- (void)startTimer
{
if(_timer == nil)
_timer = [NSTimer scheduledTimerWithTimeInterval:0.6f target:self selector:#selector(currentTimerString) userInfo:nil repeats:YES];
else
[_timer fire];
}
- (void)stopTimer
{
if(_timer)
[self.timer invalidate];
self.timer = nil;
}
#end
we are created the recipe object, this will handle the timer and notification to tableview, starting the timer and stoping the timer you can add some other functionalities if u want to.. :)
in ViewController.h file
#import <UIKit/UIKit.h>
#import "Recipe.h" //import the recipe class
#interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate,RecipeDelegate>
#property (weak, nonatomic) IBOutlet UITableView *timerWindowTbl;
#property (nonatomic, strong) NSMutableArray *recipeArr; //array to hold the recipes
#end
in ViewController.m file
#import "ViewController.h"
#import "RecipeDetailViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_recipeArr = [[NSMutableArray alloc] init];
[self populateRecipe]; //i am simply creating the recipes hear for testing
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)populateRecipe
{
for(NSInteger k = 0 ; k < 10 ; k++)
{
Recipe *recipe = [[Recipe alloc] initWithRecipieId:k recipe eName:[NSString stringWithFormat:#"recipe_%ld",(long)k]];
[self.recipeArr addObject:recipe];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//just configure your tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.recipeArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"RECIPEE_CELL"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"RECIPEE_CELL"];
}
Recipe *recipe = self.recipeArr[indexPath.row];
recipe.delegate = self;
cell.textLabel.text = recipe.recipeName;
cell.detailTextLabel.text = recipe.countTimer;
return cell;
}
//hear go to recipe detail controller and set timer
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Recipe *selRecipe = self.recipeArr[indexPath.row];
RecipeDetailViewController *recipeDetailController = [[RecipeDetailViewController alloc] initWithNibName:#"RecipeDetailViewController" bundle:nil];
recipeDetailController.selectedRecipe = selRecipe;
[self.navigationController pushViewController:recipeDetailController animated:YES];
}
//this is the delegate method from recipe class
//this method is responsible for reloading the particular cell
- (void)timerChangedInRecipe:(Recipe *)recipe
{
NSInteger index = recipe.recipeboxId;
NSIndexPath *rowPath = [NSIndexPath indexPathForRow:index inSection:0];
[self.timerWindowTbl reloadRowsAtIndexPaths:#[rowPath] withRowAnimation:UITableViewRowAnimationNone];
}
#end
and in detail controller same as your recipe detail controller RecipeDetailViewController.h file
#import <UIKit/UIKit.h>
#import "Recipe.h"
#interface RecipeDetailViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIDatePicker *timePicker;
#property (weak, nonatomic) IBOutlet UIButton *okButton;
#property (weak, nonatomic) IBOutlet UILabel *timerLabel;
#property (nonatomic, strong) Recipe *selectedRecipe;
- (IBAction)okBtn:(id)sender;
#end
and in RecipeDetailViewController .m file,
#import "RecipeDetailViewController.h"
#interface RecipeDetailViewController ()
#end
#implementation RecipeDetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[_timePicker addTarget:self action:#selector(timerChanged:) forControlEvents:UIControlEventValueChanged]; //i am setting a sample picker
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)timerChanged:(UIDatePicker *)datePickerView
{
NSTimeInterval duration = datePickerView.countDownDuration;
int hours = (int)(duration/3600.0f);
int minutes = ((int)duration - (hours * 3600))/60;
int seconds = minutes/60;
_selectedRecipe.interval = seconds + (minutes*60) + (hours*3600);
_selectedRecipe.secondsLeft =_selectedRecipe.interval;
_timerLabel.text = [NSString stringWithFormat:#"%02d:%02d:%02d", hours, minutes, seconds];
_selectedRecipe.interval = duration;
}
- (IBAction)okBtn:(id)sender //hear start the timer particular recipe
{
[_selectedRecipe startTimer];
}
#end
Edit in the source code u are shared
the problem in the cell type i think, if you want u can subclass the tableview cell and place the timer labels in the required position,
so in your code, ViewController.m file replace below method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"RECIPEE_CELL"];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"RECIPEE_CELL"];
}
Recipe *recipe = self.recipeArr[indexPath.row];
recipe.delegate = self;
cell.textLabel.text = recipe.recipeName;
cell.detailTextLabel.text = recipe.countTimer;
NSLog(#"timer in cell :%#",cell.detailTextLabel.text);
return cell;
}
and in the RecipeDetailViewController.m file replace,
//no need to wait for value change
- (IBAction)okBtn:(id)sender //hear start the timer particular recipe
{
NSTimeInterval duration = _timePicker.countDownDuration;
_selectedRecipe.interval = duration;
[_selectedRecipe startTimer];
}
and also change the timePicker mode to Count Down Timer in attribute inspector
u can download the edited code hear
Put UI related changes in main queue for better understanding show given below code. You can also put other UI related changes in Main_queue
dispatch_async(dispatch_get_main_queue(), ^{
lbl.text=updated_timer;
});
Related
I want a ranking in my app where you can see who has the fastest lap.
I try to convert the time into milliseconds, but it didn't work well.
This is my code at the moment.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize watch,start,reset;
- (void)viewDidLoad {
[super viewDidLoad];
running = NO;
count = 0;
watch.text = #"00:00.00";
start.layer.cornerRadius = 45;
reset.layer.cornerRadius = 45;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)startpressed:(id)sender {
if (running == NO) {
running = YES;
[start setTitle:#"STOPP" forState:UIControlStateNormal];
NSDate *watch = [NSDate dateWithTimeIntervalSince1970:(1273636800 / 1000.0)];
if (myTimer == nil) {
myTimer = [NSTimer scheduledTimerWithTimeInterval:0.0055
target:self
selector:#selector(updateTimer)
userInfo: nil
repeats:YES];
}
} else {
running = NO;
[myTimer invalidate];
myTimer = nil;
[start setTitle:#"START" forState:UIControlStateNormal];
}
}
- (IBAction)resetpressed:(id)sender {
running =NO;
[myTimer invalidate];
myTimer =nil;
[start setTitle:#"START" forState:UIControlStateNormal];
count = 0;
watch.text = #"00:00.00";
}
- (void)updateTimer {
count++;
int min = floor(count/100/60);
int sec = floor(count/100);
int mSec = count % 100;
if (sec >= 60) {
sec = sec % 60;
}
watch.text = [NSString stringWithFormat:#"%02d:%02d.%02d", min,sec,mSec];
}
#end
this is my ViewController.h code:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
NSTimer *myTimer;
BOOL running;
int count;
double methodStart;
}
#property (weak, nonatomic) IBOutlet UILabel *watch;
#property (weak, nonatomic) IBOutlet UIButton *start;
#property (weak, nonatomic) IBOutlet UIButton *reset;
#property (weak, nonatomic) IBOutlet UILabel *eins;
#property (weak, nonatomic) IBOutlet UILabel *zwei;
#property (weak, nonatomic) IBOutlet UILabel *drei;
- (IBAction)startpressed:(id)sender;
- (IBAction)resetpressed:(id)sender;
- (void) updateTimer;
#end
Create array for holding lap history in class interface/extension and initialise it.
self.lapHistory = [#[] mutableCopy];
Capture and sort every lap time when lap is reset/stopped
- (IBAction)resetpressed:(id)sender {
[self.lapHistory addObject:#(count)];
self.lapHistory = [self.lapHistory sortedArrayUsingSelector: #selector(compare:)];
}
Display sorted ranking from lapHistory
-(void)displayRanking{
NSMutableString *rankingResult = [[NSMutableString alloc] init];
for (NSNumber *lap in self.lapHistory) {
[rankingResult appendString:[NSString stringWithFormat:#"%ld\n",[lap integerValue]]];
}
NSLog(#"Ranking result is %#", rankingResult);
}
I have two tableViewControllers, second tableViewController tableViewCell has textField and imageView, how can I get data from that textfield when I click navigationBarBackButton (I want to store that data in first tableViewController).
// code from second tableViewController...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 0) {
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"cellID"];
cell.textLabel.text = #"SIM Name";
cell.detailTextLabel.text = #"AirTel";
cell.imageView.image = [UIImage imageNamed:#"Star2.png"];
return cell;
}
TableViewCell1 *cell1 = [tableView dequeueReusableCellWithIdentifier:#"cell1"];
cell1.nameTextField.delegate = self;
if (indexPath.row == 1) {
NSArray *cell1XibRef = [[NSBundle mainBundle]loadNibNamed:#"TableViewCell1" owner:self options:nil];
cell1 = [cell1XibRef objectAtIndex:0];
cell1.imageView.image = [UIImage imageNamed:#"Con.png"];
self.nameString = cell1.nameTextField.text;
return cell1;
}
return nil;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
//Whenever people start editing your textfield
self.nameString = textField.text;
NSLog(#"%#", self.nameString);
}
So many ways to do that but I prefer this way
Save the textfield.text value in NSUserDefaults like this
[[NSUserDefaults standardUserDefaults] setObject:textField.text forKey:#"controllerTwo"];
And get that value in first controller
NSString *secondContrllerText = [[NSUserDefaults standardUserDefaults] stringForKey:#"controllerTwo"];
You can directly add cells in one array and loads cells from that array which will returns you latest cell with all your edits.
NSMutableArray *arrCells = [[NSMutableArray alloc] init];
for (init i=0; i<20; i++){
UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:#"cellID"];
arrCells = cell;
}
In cellForRowAtIndexPath method:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = arrCell[indexPath.row];
......
return cell;
}
here is the code i like to pass the data with block.here is the code wish to help you.
FirstViewController.h
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController
#end
FirstViewController.m
#import "FirstViewController.h"
#import "ViewController.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)buttonClick
{
ViewController *controller = [[ViewController alloc] init];
controller.passTheTextFieldData = ^(NSString *textValue){
NSLog(#"show the %# textValue",textValue);
};
[self.navigationController pushViewController:controller animated:YES];
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (nonatomic, strong) void (^passTheTextFieldData)();
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()<UITextFieldDelegate>
#property (nonatomic, strong) UITextField *textField;
#property (nonatomic, strong) NSString *textValue;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[self.textField addTarget:self action:#selector(textFieldValueChanged) forControlEvents:UIControlEventValueChanged];
}
- (void)textFieldValueChanged
{
self.textValue = self.textField.text;
}
- (void)navigationbarBackClick
{
if (self.passTheTextFieldData) {
self.passTheTextFieldData(self.textValue);
}
}
#end
Set the tag property for textField in tableViewCell
Then loop through the cells
for (int i = 0 ; i < [cellDataArray count] ; i++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
UITextField *txtfield = [[cell contentView]viewWithTag:2];
//now get the text txtfield.text
}
Hope this helps
Either you can use delegate method to store your value or use NSUserdefaults.But you have to store one value to pass it to another table view i recommend you to use NSUserdefaults.
//suppose you have to save text (get your data)
NSString *valueToSave = cell1.nameTextField.text;
[[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:#"keyAbcd"];
[[NSUserDefaults standardUserDefaults] synchronize];
//another page where you want to show your data (paste your data)
NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:#"keyAbcd"];//your key have to be same
//print save value in nslog
I'm using https://github.com/mineschan/MZTimerLabel/
and in my Tableview cellForRowAtIndex using the timer like below:
UILabel *lblTimer=(UILabel *)[cell viewWithTag:10];
MZTimerLabel *UpgradeTimer = [[MZTimerLabel alloc] initWithLabel:lblTimer andTimerType:MZTimerLabelTypeTimer];
[UpgradeTimer setCountDownTime:timestamp];
[UpgradeTimer startWithEndingBlock:^(NSTimeInterval timestamp) {
lblTimer.text = #"✔";
}];
But after any table reloading or scrolling, the timer behaves strange and seems it re-generates multiple timers for counting in the same place.
How should I fix this while using this timer?
Appreciate any help,
Elias
I had a look at MZTimerLabel, and it violates MVC badly. It puts something that belongs into the model (the timer that count's down the time) into the view. That is where your problem comes from. Views should be able to be recreated without having side effects on the model.
I would recommend to ditch that class, and create your own. It's actually quite easy to achieve something like this.
Create a new class that saves a title and a endDate
Store instances of that class in the model that backs your table
Create one NSTimer that refreshes the tableView
Set up your cells.
That's basically all the code you need for a basic countdown in a table. Because it does not store any data in the view you can scroll as much as you like:
#interface Timer : NSObject
#property (strong, nonatomic) NSDate *endDate;
#property (strong, nonatomic) NSString *title;
#end
#implementation Timer
#end
#interface MasterViewController () {
NSArray *_objects;
NSTimer *_refreshTimer;
}
#end
#implementation MasterViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *modelStore = [NSMutableArray arrayWithCapacity:30];
for (NSInteger i = 0; i < 30; i++) {
Timer *timer = [[Timer alloc] init];
timer.endDate = [NSDate dateWithTimeIntervalSinceNow:i*30];
timer.title = [NSString stringWithFormat:#"Timer %ld seconds", (long)i*30];
[modelStore addObject:timer];
}
_objects = modelStore;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[_refreshTimer invalidate]; // timer should not exist, but just in case.
_refreshTimer = [NSTimer timerWithTimeInterval:0.5f target:self selector:#selector(refreshView:) userInfo:nil repeats:YES];
// should fire while scrolling, so we need to add the timer manually:
[[NSRunLoop currentRunLoop] addTimer:_refreshTimer forMode:NSRunLoopCommonModes];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[_refreshTimer invalidate];
_refreshTimer = nil;
}
- (void)refreshView:(NSTimer *)timer {
// only refresh visible cells
for (UITableViewCell *cell in [self.tableView visibleCells]) {
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
[self configureCell:cell forRowAtIndexPath:indexPath];
}
}
#pragma mark - Table View
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _objects.count;
}
- (void)configureCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
Timer *timer = _objects[indexPath.row];
cell.textLabel.text = timer.title;
NSInteger timeUntilEnd = (NSInteger)[timer.endDate timeIntervalSinceDate:[NSDate date]];
if (timeUntilEnd <= 0) {
cell.detailTextLabel.text = #"Finished";
}
else {
NSInteger seconds = timeUntilEnd % 60;
NSInteger minutes = (timeUntilEnd / 60) % 60;
NSInteger hours = (timeUntilEnd / 3600);
cell.detailTextLabel.text = [NSString stringWithFormat:#"%02ld:%02ld:%02ld", (long)hours, (long)minutes, (long)seconds];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
[self configureCell:cell forRowAtIndexPath:indexPath];
return cell;
}
#end
In my iOS app I have a custom UITableViewCell that is part of a TableView in my FirstViewController. In the FirstViewController I have an array of objects that corresponds to populating the rows and then of course the objects in that array which have properties.
I have a timer in each cell and I need to get the length of the timer from a property of the corresponding object. I've tried importing FirstViewController.h but I get the error "property not found on object of type" no matter what.
I already have the object created and configured in the cellForRowAtIndexPath method of the FirstViewController but I was hoping to use it in a method in TableViewCell.m.
Is there a way I can use the array or the object in the TableViewCell? Or should the method be implemented in the FirstViewController?
EDIT #1:
I moved cell.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:cell selector:#selector(startTimer) userInfo:nil repeats:YES]; into the cellForRow... method of the FirstViewController. Now the timer runs with the desired time but it's no longer started by a button. It immediately runs when the TableView is loaded and restarts every time the TableView is reloaded.
Here is my ATCTableViewCell.h file: (Updated with Edit #2)
#import <UIKit/UIKit.h>
#import "ATCFirstViewController.h"
#interface ATCTableViewCell : UITableViewCell
- (void)startTimer;
// Contents moved to cellForRow... -> - (IBAction)playTimer:(id)sender;
#property (weak, nonatomic) IBOutlet UILabel *titleLabel;
#property (weak, nonatomic) IBOutlet UILabel *timeLabel;
#property (weak, nonatomic) IBOutlet UIButton *playButton;
#property (weak, nonatomic) IBOutlet UIButton *pauseButton;
#property NSTimer *timer;
#property int secondsCount; // initially the length in seconds and then decreased
#end
EDIT #2:
The timer is a decrementing timer that changes a label in the startTimer method which is in ATCTableViewCell.m. The label is a property of the cell at the moment along with another label and the buttons. I'll work on moving things to the object class instead of having them as properties the cell.
Here is the startTimer method:
- (void)startTimer {
self.secondsCount--;
int hours = self.secondsCount / 3600;
int minutes = (self.secondsCount / 60) - (hours * 60);
int seconds = self.secondsCount - (hours * 3600) - (minutes * 60);
NSString *timerOutput = [NSString stringWithFormat:#"%02d:%02d:%02d", hours, minutes, seconds];
self.timeLabel.text = timerOutput;
if (self.secondsCount == 0) {
[self.timer invalidate];
self.timer = nil;
}
}
Here is my ATCObject.h file:
#import <Foundation/Foundation.h>
#interface ATCObject : NSObject
#property NSString *title;
#property int lengthInSeconds;
#property int initialHours;
#property int initialMinutes;
#end
Thanks
I am updating this answer based on all of the previous discussion.
#interface ATCObject : NSObject
#property (nonatomic, strong) NSString *title;
#property (nonatomic, assign) NSInteger lengthInSeconds;
#property (nonatomic, assign) NSInteger initialHours;
#property (nonatomic, assign) NSInteger initialMinutes;
//Move the NSTimer and the -playTimer method to this class
#property (nonatomic,strong) NSTimer *timer;
-(void)startTimer;
-(void)timerTick;
-(void)pauseTimer;
#end
In your tableView controller class:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Identifier";
ATCTableViewCell *cell = (ATCTableViewCell *)[tableView dequeueReusableCellForIdentifer:Identifier];
if (!cell) {
cell = [[ATCTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault identifier:Identifier];
[cell.playButton addTarget:self action:#selector(playTouched:) forControlEvents:UIControlEventTouchUpInside];
[cell.pauseButton addTarget:self action:#selector(pauseTouched:) forControlEvents:UIControlEventTouchUpInside];
}
ATCObject *objectForRow = [myDataArray objectAtIndex:indexPath.row];
cell.timeLabel.text = objectForRow.title;
return cell;
}
//Also in your controller class
-(void)playTouched:(id)sender {
//you may need to change self.tableView to however you reference the table in your class
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:buttonPosition];
if (indexPath != nil) {
ATCObject *currentObject = [myDataArray objectAtIndex:indexPath.row];
[currentObject startTimer];
}
}
//ATCObject.m
-(void)startTimer {
if (!self.timer) {
self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(timerTick:)
userInfo:nil
repeats:YES];
}
}
-(void)timerTick {
//Already assuming lengthInSeconds is set
if (lengthInSeconds > 0) {
lengthInSeconds--;
//Do your math to get total hours and minutes
NSInteger totalHours = //math stuff convert lengthInSeconds
NSInteger totalMinutes = //more math stuff etc
NSString *newTitle = [NSString stringWithFormat:#"%i:%i",totalHours,totalMinutes];
self.title = newTitle;
} else {
self.title = #"00:00";
[self.timer invalidate];
self.timer = nil;
}
}
I'm using https://github.com/mineschan/MZTimerLabel/
and in my Tableview cellForRowAtIndex using the timer like below:
UILabel *lblTimer=(UILabel *)[cell viewWithTag:10];
MZTimerLabel *UpgradeTimer = [[MZTimerLabel alloc] initWithLabel:lblTimer andTimerType:MZTimerLabelTypeTimer];
[UpgradeTimer setCountDownTime:timestamp];
[UpgradeTimer startWithEndingBlock:^(NSTimeInterval timestamp) {
lblTimer.text = #"✔";
}];
But after any table reloading or scrolling, the timer behaves strange and seems it re-generates multiple timers for counting in the same place.
How should I fix this while using this timer?
Appreciate any help,
Elias
I had a look at MZTimerLabel, and it violates MVC badly. It puts something that belongs into the model (the timer that count's down the time) into the view. That is where your problem comes from. Views should be able to be recreated without having side effects on the model.
I would recommend to ditch that class, and create your own. It's actually quite easy to achieve something like this.
Create a new class that saves a title and a endDate
Store instances of that class in the model that backs your table
Create one NSTimer that refreshes the tableView
Set up your cells.
That's basically all the code you need for a basic countdown in a table. Because it does not store any data in the view you can scroll as much as you like:
#interface Timer : NSObject
#property (strong, nonatomic) NSDate *endDate;
#property (strong, nonatomic) NSString *title;
#end
#implementation Timer
#end
#interface MasterViewController () {
NSArray *_objects;
NSTimer *_refreshTimer;
}
#end
#implementation MasterViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *modelStore = [NSMutableArray arrayWithCapacity:30];
for (NSInteger i = 0; i < 30; i++) {
Timer *timer = [[Timer alloc] init];
timer.endDate = [NSDate dateWithTimeIntervalSinceNow:i*30];
timer.title = [NSString stringWithFormat:#"Timer %ld seconds", (long)i*30];
[modelStore addObject:timer];
}
_objects = modelStore;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[_refreshTimer invalidate]; // timer should not exist, but just in case.
_refreshTimer = [NSTimer timerWithTimeInterval:0.5f target:self selector:#selector(refreshView:) userInfo:nil repeats:YES];
// should fire while scrolling, so we need to add the timer manually:
[[NSRunLoop currentRunLoop] addTimer:_refreshTimer forMode:NSRunLoopCommonModes];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[_refreshTimer invalidate];
_refreshTimer = nil;
}
- (void)refreshView:(NSTimer *)timer {
// only refresh visible cells
for (UITableViewCell *cell in [self.tableView visibleCells]) {
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
[self configureCell:cell forRowAtIndexPath:indexPath];
}
}
#pragma mark - Table View
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _objects.count;
}
- (void)configureCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
Timer *timer = _objects[indexPath.row];
cell.textLabel.text = timer.title;
NSInteger timeUntilEnd = (NSInteger)[timer.endDate timeIntervalSinceDate:[NSDate date]];
if (timeUntilEnd <= 0) {
cell.detailTextLabel.text = #"Finished";
}
else {
NSInteger seconds = timeUntilEnd % 60;
NSInteger minutes = (timeUntilEnd / 60) % 60;
NSInteger hours = (timeUntilEnd / 3600);
cell.detailTextLabel.text = [NSString stringWithFormat:#"%02ld:%02ld:%02ld", (long)hours, (long)minutes, (long)seconds];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
[self configureCell:cell forRowAtIndexPath:indexPath];
return cell;
}
#end