각 셀에 버튼을 위치시키고,
각 버튼에는 이벤트를 달아 놓는다
셀에 위치한 시작버튼을 눌렀을때,
타이머가 작동할 수 있는 이벤트가 실행된다
=========================================================================
[cellButton setTitle:@"시작" forState:0];
[cellButton addTarget:self action:@selector(cellButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
=========================================================================
버튼이 눌렸다면, 이벤트가 발생이 되고
이벤트 발생시에, 각 셀에 대한 타이머를 작동시킨다
=========================================================================
-(void)cellButtonClicked:(id)sender
{
int selectedTag = (int)[sender tag];
NSMutableArray *info = [[NSMutableArray alloc] init];
NSNumber *cost = [[NSNumber alloc] initWithInt:selectedTag];
[info addObject:cost];
// 타이머 셋팅 시작 - 개별 선택된 셀의 타이머가 따로 작동한다
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerMethod:) userInfo:info repeats:YES];
}
=========================================================================
타이머는 또 다른 메서드를 콜하게 된다
해당 메서드에서는 시간을 1초씩 감소시키는 작업을 하고,
타이머 종료시에 알람을 메시지 창으로 띄우는 역할을 한다
=========================================================================
-(void) timerMethod:(NSTimer *)timer
{
NSNumber *cost = [[timer userInfo] objectAtIndex:0];
int theTag = [cost intValue];
NSInteger a = [dataArray[theTag][1] integerValue];
a = a - 1;
NSString *temp = [NSString stringWithFormat:@"%ld", (long)a];
// 시간값을 바꾸어 준다
[[dataArray objectAtIndex:theTag] replaceObjectAtIndex:1 withObject:temp];
// 미리 정의 된 데이터 파일로 아카이브
[NSKeyedArchiver archiveRootObject:dataArray toFile:dataFilePath];
// 타이머 종료 시, 알람메시지를 띄운다
if( 0 == [dataArray[theTag][1] integerValue])
{
NSString *temp2 = [NSString stringWithFormat:@"%d", theTag];
UIAlertView *AlrmView = [[UIAlertView alloc] initWithTitle:@"알람시계" message:temp2 delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", @"") otherButtonTitles:nil];
[AlrmView show];
// 타이머를 종료한다
[timer invalidate];
}
// 화면 재로드
[self ReloadRecordList];
}
=========================================================================
'IT > iPhone' 카테고리의 다른 글
ios에서 테이블뷰에서 커스터마이징 된 셀을 삭제하는 법 (0) | 2016.08.13 |
---|---|
ios에서 테이블뷰에 셀 커스터마이징하여 위치시키기 (0) | 2016.06.23 |
ios에서 에러 발생 - tableView commitEditingStyle: (0) | 2016.05.10 |
ios에서 타이머에 스레드를 사용하는 방법 _ 검색편 (0) | 2016.05.09 |
ios에서 타이머 기능을 추가하기 _ 검색편 (0) | 2016.05.08 |