테이블 뷰 컨트롤러와 셀로 연결된 세부보기 컨트롤러가 & 네비게이션 바있다. 자세히보기 컨트롤러에서 사용자가 지정한 간격으로 카운트 다운 타이머입니다. 셀을 처음 클릭 할 때 디테일 뷰 컨트롤러를 가져올 때 작동하지만 같은 셀을 탭하고 다시 탭하면 처음부터 다시 시작합니다. 나는 이유를 안다. 그러나 나는 그것을 고칠 방법을 모른다 (이 포스트의 끝에). 여기 내 코드입니다 :의 tableview 세포에 넣어사과의 카운트 다운 타이머와 같은 타이머를 만드는 법을 모르겠다.
작업 = 객체
되지 않은 작업의 배열이
완료 작업의completedArray = 배열 객체
TableViewController.m
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
cellSubclassCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
if (!cell)
cell = [[cellSubclassCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell"];
if([indexPath section] == 0){
cell.textLabel.text = [[[self.taskArray objectAtIndex:[indexPath row]] taskName] uppercaseString];
cell.imageView.image = [UIImage imageNamed:@"unchecked.png"];
cell.imageView.highlightedImage = [UIImage imageNamed:@"uncheckedhighlighted.png"];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
[cell setBackgroundColor:[UIColor colorWithRed:236.0/255 green:240.0/255 blue:241.0/255 alpha:1.0f]];
cell.textLabel.textColor = baseColor;
NSString *detailText = [[self.taskArray objectAtIndex:[indexPath row]] timeIntervalString];
cell.detailTextLabel.text = detailText;
[[cell detailTextLabel] setFont:[UIFont fontWithName:@"Avenir-Black" size:12]];
[[cell textLabel] setFont:[UIFont fontWithName:@"AvenirNext-DemiBold" size:16]];
[cell.contentView setAlpha:1];
} else if ([indexPath section] == 1) {
cell.textLabel.text = [[[self.completedArray objectAtIndex:[indexPath row]] taskName] uppercaseString];
cell.imageView.image = [UIImage imageNamed:@"checked.png"];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
[cell setBackgroundColor:[UIColor colorWithRed:236.0/255 green:240.0/255 blue:241.0/255 alpha:1.0f]];
cell.textLabel.textColor = baseColor;
NSString *detailText = [[self.completedArray objectAtIndex:[indexPath row]] timeIntervalString];
cell.detailTextLabel.text = detailText;
[[cell detailTextLabel] setFont:[UIFont fontWithName:@"Avenir-Black" size:12]];
[[cell textLabel] setFont:[UIFont fontWithName:@"AvenirNext-DemiBold" size:16]];
[cell.contentView setAlpha:0.5];
}
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handlechecking:)];
//cell.contentView
[cell.imageView addGestureRecognizer:tap];
cell.imageView.userInteractionEnabled = YES;
return cell;
}
-(void)handlechecking:(UITapGestureRecognizer *)t{
CGPoint tapLocation = [t locationInView:self.tableView];
NSIndexPath *tappedIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation];
NSIndexPath *newIndexPath = nil;
if (tappedIndexPath.section == 0) {
NSUInteger newRowIndex = self.completedArray.count;
[self.completedArray addObject:[self.taskArray objectAtIndex:tappedIndexPath.row]];
[self.taskArray removeObject:[self.taskArray objectAtIndex:tappedIndexPath.row]];
newIndexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:1];
} else {
NSUInteger newRowIndex = self.taskArray.count;
[self.taskArray addObject:[self.completedArray objectAtIndex:tappedIndexPath.row]];
[self.completedArray removeObject:[self.completedArray objectAtIndex:tappedIndexPath.row]];
newIndexPath = [NSIndexPath indexPathForRow:newRowIndex inSection:0];
}
[self.tableView beginUpdates];
[self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView deleteRowsAtIndexPaths:@[tappedIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSInteger num = 0;
if (section == 0) {
num = self.taskArray.count;
} else {
num = self.completedArray.count;
}
return num;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
Tasks *task = [[Tasks alloc]init];
if (indexPath.section == 0){
task.taskName = [[self.taskArray objectAtIndex:[indexPath row]] taskName];
task.timeInterval = [[self.taskArray objectAtIndex:[indexPath row]] timeInterval];
task.dateCreated = [[self.taskArray objectAtIndex:[indexPath row]] dateCreated];
} else if (indexPath.section == 1){
task.taskName = [[self.completedArray objectAtIndex:[indexPath row]] taskName];
task.timeInterval = [[self.completedArray objectAtIndex:[indexPath row]] timeInterval];
task.dateCreated = [[self.completedArray objectAtIndex:[indexPath row]] dateCreated];
}
DetailViewController *dvc = [[DetailViewController alloc]init];
[dvc setTestTask:task];
[[self navigationController] pushViewController:dvc animated:YES];
}
객체 = taskArray
DetailViewController.m
-(IBAction)startTimer:(id)sender{
[sender setHidden:YES];
[pauseButton setHidden:NO];
[continueButton setHidden:NO];
gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
self.date1 = [NSDate dateWithTimeInterval:[testTask timeInterval] sinceDate:[NSDate date]];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
[timer fire];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[timerLabel setFont:[UIFont fontWithName:@"BebasNeue" size:60]];
[[self navigationItem] setTitle:[testTask taskName]];
if (startButton.hidden == NO){
[pauseButton setHidden:YES];
[continueButton setHidden:YES];
} else {
[pauseButton setHidden:NO];
[continueButton setHidden:NO];
}
timeRemaining = [NSString stringWithFormat:@"%02d:%02d:%02d", [components hour], [components minute], [components second]];
timerLabel.text = timeRemaining;
[timerLabel setNeedsDisplay];
[self.view setNeedsDisplay];
}
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
[testTask setTaskName:[testTask taskName]];
[testTask setTimeInterval:[testTask timeInterval]];
}
-(void)timerAction:(NSTimer *)t{
NSDate *now = [NSDate date];
components = [gregorianCalendar components:NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit fromDate:now toDate:self.date1 options:0];
timeRemaining = nil;
if([now compare:self.date1] == NSOrderedAscending){
timeRemaining = [NSString stringWithFormat:@"%02d:%02d:%02d", [components hour], [components minute], [components second]];
NSLog(@"works %@", timeRemaining);
} else {
timeRemaining = [NSString stringWithFormat:@"00:00:00"];
[self.timer invalidate];
self.timer = nil;
if (self.alertView == NULL){
self.alertView = [[UIAlertView alloc]initWithTitle:[testTask taskName] message:@"Time is up!" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
NSLog(@"ended");
}
}
timerLabel.text = timeRemaining;
[timerLabel setNeedsDisplay];
[self.view setNeedsDisplay];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
self.alertView = NULL;
}
기본적으로 응용 프로그램이 백그라운드에있을 때 타이머가 계속되어야합니다 (계산 된 시간에 따라 경고가 전송됩니다). 다시 말해서 셀을 다시 클릭 할 때 타이머가 계속되어야 함을 의미합니다. 이 작업을 수행하지 않는 이유는 셀 (또는 행)을 누를 때마다 DetailViewController가 할당되기 때문입니다. 나는 이것을 고치는 방법을 모르므로 시간을 절약하고 내가 좋아하는대로 시작/일시 정지 할 수 있습니다.
NSUserDefaults 또는 CoreData를 사용한 적이 없습니다. 시간을 절약하는 것이 쉬운가요? – EvilAegis
NSUserDefaults는 사용자 기본 설정과 같은 시스템 기본값을 저장하는 데 사용된다고 가정합니다. 기술적으로 당신이 필요로하는 것을하기위한 것이 아니므로 CoreData를 사용하도록 개인적으로 제안 할 것입니다. 약간의 학습 곡선이 있지만 귀하의 요구에 따라 나는 당신이 그것을 집어 들고 며칠 안에 그것을 구현할 수있을 것이라고 확신합니다. – random
확인. 2.1 단계에서 핵심 데이터가 정확합니까? – EvilAegis