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