2013-04-03 55 views
2

**我在單元格上顯示倒計時器,當我長按cell.but但問題是,當我點擊行定時器開始,但突然當我點擊第二個單元計時器停止前一個單元格,並啓動計時器新細胞,我點擊,但我需要的主要功能是我必須顯示運行前一個計時器和當前計時器。請任何人給我這種權利的方式。**如何在iphone中顯示細胞倒計時器?

-(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]; 
     [email protected]""; 
     // 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; 
    [email protected]""; 
    [self SendRequsetToServerOfSeenImage]; 

} 
else 
{ 
} 


} 
+0

我想實現相同的運行多個定時器,你成功了嗎? –

回答

0

的問題是,你只存儲一個定時器/ indexpath對(你只使用變量「定時器」, 「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]; 
真正的索引路徑
+0

[__NSCFTimer copyWithZone:]:無法識別的選擇器發送到實例0xa2c9000 2013-04-04 13:12:39.604 SnapChat [996:c07] ***由於未捕獲異常'NSInvalidArgumentException',原因:' - [__ NSCFTimer copyWithZone :]:無法識別的選擇發送到實例0xa2c9000' – IOSDev

+0

我添加你的代碼在我的應用程序,但它的崩潰 – IOSDev

+0

我添加了你的代碼,但它不工作。 – IOSDev

0

試試這個

- (void)countDownTimer 
{ 

timer =[NSTimer scheduledTimerWithTimeInterval:1.0 target:self   selector:@selector(updateCounter:) userInfo:nil repeats:YES]; 
[[NSRunLoop mainRunLoop]addTimer:timer forMode:NSRunLoopCommonModes]; 

} 
+0

我試試這個,但之前的定時器停在當前位置,第二個單元定時器開始。 – IOSDev

0

我想你想在視圖上或每個單元上運行多個定時器。但處理運行在不同單元上的多個定時器會變得很複雜。