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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…