iphone - 如何在iPhone的手机上显示倒数计时器?

标签 iphone ios objective-c ipad

**当我长按cell时,我会在该cell上显示倒数计时器。但是问题是,当我单击开始的行计时器时,却突然单击了上一个cell的第二个cell timer stop并启动了我单击的新的timer时,但是我需要的主要功能是必须显示正在运行的上一个计时器和当前计时器。请任何人为此给我正确的方法。**

 -(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
{
}


}

最佳答案

问题是您只存储一对计时器/索引路径(仅使用变量“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];

关于iphone - 如何在iPhone的手机上显示倒数计时器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15786803/

相关文章:

iphone - 从 UIViewController 导航回来导致崩溃

苹果手机 : click on google Admob restarts the device

ios - 如何向 NSManagedObject 发送电子邮件

ios - 在 Swift 属性包装器中支持可选参数值

objective-c - 如何使用 NSNumberFormatter 格式化 UITextField?

iOS UnitySendMessage 调用参数

objective-c - Swift:实现协议(protocol)的类的类型

iphone - 将项目从 UITableView 拖放到 UITableView

iTunesConnect 上的 iOS 应用程序图标未显示

ios - Xamarin iOS : Doesn't make any sound using AVSpeechSynthesizer?