如何在iphone中显示手机上的倒计时?
**当我长按单元格时,我会在单元格上显示倒计时。但问题是,当我单击行计时器启动时,但当我单击上一个单元格的第二个单元格计时器停止并启动我单击的新单元格计时器时,会突然启动计时器,但我需要的主要功能是,我必须显示正在运行的前一个计时器和当前计时器。请任何人给我正确的方法。**如何在iphone中显示手机上的倒计时?,iphone,ios,objective-c,ipad,Iphone,Ios,Objective C,Ipad,**当我长按单元格时,我会在单元格上显示倒计时。但问题是,当我单击行计时器启动时,但当我单击上一个单元格的第二个单元格计时器停止并启动我单击的新单元格计时器时,会突然启动计时器,但我需要的主要功能是,我必须显示正在运行的前一个计时器和当前计时器。请任何人给我正确的方法。** -(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer { AppDelegate *app=(AppDelegate *)[UI
-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{ AppDelegate *app=(AppDelegate *)[UIApplication sharedApplication].delegate;
CGPoint p = [gestureRecognizer locationInView:ChatTable];
NSIndexPath *indexPath = [ChatTable indexPathForRowAtPoint:p];
if ([[[userArray valueForKey:@"sender_id"] objectAtIndex:indexPath.row]isEqualToString: app.userId]) {
}
else{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
{
if ([imageCache objectForKey:[NSString stringWithFormat:@"%d",indexPath.row]]==NULL) {
}
else
{
self.picId=[[userArray valueForKey:@"pic_id"] objectAtIndex:indexPath.row];
self.imageName=[[userArray valueForKey:@"imagename"] objectAtIndex:indexPath.row];
ChatTable.userInteractionEnabled=NO;
// self.navigationController.navigationBarHidden=YES;
image=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
self.pickerValue=[[userArray valueForKey:@"seconds"] objectAtIndex:indexPath.row];
image.image=[imageCache objectForKey:[NSString stringWithFormat:@"%d",indexPath.row]];
[self.view addSubview:image];
[self showTableTime:indexPath];
}
}
else if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
UITableViewCell * myCell = [ChatTable cellForRowAtIndexPath:indexpathForTimer];
UILabel * myLabel = (UILabel *)[myCell viewWithTag:101];
myLabel.text=@"";
// self.navigationController.navigationBarHidden=NO;
[image removeFromSuperview];
}
}
}
-(void)showTableTime:(NSIndexPath *)indexPath
{
secondsLeft=[self.pickerValue integerValue];
indexpathForTimer=indexPath;
[self countDownTimer];
}
- (void)countDownTimer {
timer =[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
}
- (void)updateCounter:(NSTimer *)theTimer {
UITableViewCell * myCell = [ChatTable cellForRowAtIndexPath:indexpathForTimer];
UILabel * myLabel = (UILabel *)[myCell viewWithTag:101];
if (secondsLeft >0) {
secondsLeft --;
UILabel * myLabel = (UILabel *)[myCell viewWithTag:101];
myLabel.text=[NSString stringWithFormat:@"0%d",secondsLeft];
}
else if(secondsLeft==0)
{
[self.image removeFromSuperview];
[timer invalidate];
ChatTable.userInteractionEnabled=YES;
myLabel.text=@"";
[self SendRequsetToServerOfSeenImage];
}
else
{
}
}
试试这个
- (void)countDownTimer
{
timer =[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
}
问题是您只存储了一个timer/indexpath对(您只使用变量“timer”和“indexPathForTimer”)。一旦激活第二个单元格,变量indexPathForTimer将被第二个单元格的indexPath覆盖,因此只更新第二个单元格 我建议您将变量计时器转换为NSMutableArray,将indexPathForTimer转换为NSMapTable,然后执行以下操作
// code to init the map table, perform this where you initialize your variables
// (you may want to use weakToStrong or weakToWeak depending on your memory
// management)
indexPathForTimer = [NSMapTable strongToStrongObjectsMapTable];
// use this code to create your timer instance with reference to the index path
// for which it was instantiated
NSTimer* newTimer = [NSTimer timerWithTimerInterval:1.0 target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
[timer addObject:newTimer];
[indexPathForTimer addObject:indexPath forKey:newTimer];
[NSRunLoop mainRunLoop] addTimer:newTimer forModes:NSRunLoopCommonModes];
在updateCounter:方法中,您可以像这样获得实际的索引路径
currentIndexPath = [indexPathForTimer objectForKey:theTimer];
我认为您希望在视图或每个单元格上运行多个计时器。但处理在不同单元上运行的多个计时器会变得复杂。我尝试了此操作,但前一个计时器在当前位置停止,第二个单元计时器开始。[[uuu\NSCFTimer copyWithZone:]:未识别的选择器发送到实例0xa2c9000 2013-04-04 13:12:39.604 SnapChat[996:c07]***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[\uu NSCFTimer copyWithZone:]:无法识别的选择器发送到实例0xa2c9000'抱歉,我忘记了NSMutableDictionary需要NSCopying。我更新了代码以使用映射表。[indexPath-fortimer-addObject:indexPath-forKey:newTimer];当我应用代码[indexPathForDictionaryTimer setObject:indexPath-forKey:newTimer]时,indexPathForTimer是dictionary,我无法在dictionary中添加object;它发出警告,请通过更改这一行[indexPathForDictionaryTimer setObject:indexPath-forKey:newTimer]来帮助mei应用您的逻辑;但以前的单元卡住了,新单元由此代码启动。请告诉我应该怎么做?我想实现相同的功能以运行多个计时器。您成功完成了吗?