这是我的问题:我有一个带有一堆单元格的表格视图。Core Data 使用 NSFetchedResultsController 将 Task 对象加载到单元格中。现在我有了它,所以每个单元格都有一个 DetailViewController,它存储在字典中,如下所示:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetailViewController *detailVC;
if (![self.detailViewsDictionary.allKeys containsObject:@(indexPath.row)]){
detailVC = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
[self.detailViewsDictionary setObject:detailVC forKey:@(indexPath.row)];
}else{
detailVC = self.detailViewsDictionary[@(indexPath.row)];
}
Tasks *task = [[self fetchedResultsController] objectAtIndexPath:indexPath];
detailVC.testTask = task;
[[self navigationController] pushViewController:detailVC animated:YES];
}
每个 DetailViewController 都有一个 timer 属性,该属性从 Task 对象获取其时间间隔,该对象与 tableview 中任何给定索引路径处的单元格相关。这是我的详细视图控制器的代码:
- (void)viewDidLoad {
[super viewDidLoad];
[NSThread detachNewThreadSelector:@selector(startTimer:) toTarget:self withObject:nil];
}
-(IBAction)startTimer:(id)sender{
//default value of showButtonValue when first loaded is 1
if (testTask.showButtonValue == 1) {
[startButton setTitle:@"Start" forState:UIControlStateNormal];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
testTask.showButtonValue = 3;
} else if (testTask.showButtonValue == 2) {
[startButton setTitle:@"Resume" forState:UIControlStateNormal];
[timer invalidate];
timer = nil;
testTask.showButtonValue = 3;
} else if (testTask.showButtonValue == 3){
[startButton setTitle:@"Pause" forState:UIControlStateNormal];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
testTask.showButtonValue = 2;
}
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
nameTextField.text = testTask.taskName;
[timerLabel setFont:[UIFont fontWithName:@"Arial" size:60]];
NSUInteger seconds = (NSUInteger)round(testTask.timeInterval);
NSString *string = [NSString stringWithFormat:@"%02u:%02u:%02u",
seconds / 3600, (seconds / 60) % 60, seconds % 60];
timerLabel.text = string;
[[self navigationItem] setTitle:[testTask taskName]];
[timerLabel setNeedsDisplay];
[self.view setNeedsDisplay];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
testTask.taskName = nameTextField.text;
}
-(void)timerAction:(NSTimer *)t
{
if(testTask.timeInterval == 0)
{
if (self.timer)
{
[self timerExpired];
[self.timer invalidate];
self.timer = nil;
}
}
else
{
testTask.timeInterval--;
}
NSUInteger seconds = (NSUInteger)round(testTask.timeInterval);
NSString *string = [NSString stringWithFormat:@"%02u:%02u:%02u",
seconds / 3600, (seconds / 60) % 60, seconds % 60];
timerLabel.text = string;
NSLog(@"%f", testTask.timeInterval);
}
无论细节视图控制器当前是否在屏幕上,计时器都应该继续运行。当我第一次点击一个单元格时,转到详细视图控制器并启动计时器,然后返回到表格视图,没有问题并且倒计时继续进行。问题是,如果我重新点击单元格,则会创建另一个计时器,因此时间会以 2 倍的速度递减!不仅如此,显示剩余时间的字符串不会在 ViewWillAppear 时更新,它只会在第一次调用 timerAction 时更新。
有一堆问题,我真的很感激一些帮助!