2011-08-13 36 views
0

我想在UITableViewCell中设置一个UIButton的时间。当我点击一个按钮时它正在工作,但是当我点击2个或更多按钮时,定时器不会停止。从UITableViewCell按钮设置计时器

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    tabletTextLabel.text=[appDelegate.tabletArray objectAtIndex:indexPath.row]; 
    noOfTabletTextLabel.text=[appDelegate.noOfTabletsArray objectAtIndex:indexPath.row]; 



[cellButton setImage:[appDelegate.imageArray objectAtIndex:indexPath.row] forState:UIControlStateNormal]; 
    [cellButton addTarget:self action:@selector(CellButtonClick:) forControlEvents:UIControlEventTouchUpInside]; 
    cellButton.tag = indexPath.row; 


// cell.accessoryView = cellButton; 
    [cell.contentView addSubview:presTextLabel]; 
    [cell.contentView addSubview:tabletTextLabel]; 
    [cell.contentView addSubview:noOfTabletTextLabel]; 
    [cell.contentView addSubview:cellButton]; 
     return cell; 

}

-(void)CellButtonClick:(id)sender 
    { 

     UIButton *button = (UIButton *)sender; 
     NSLog(@"%d", button.tag); 

     [self StartTimer]; 

    } 



-(void)StartTimer 
    { 
     timer=[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(SetTabletAlert) userInfo:nil repeats:YES]; 
    } 



    -(void)SetTabletAlert 
    { 
     UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Reminder" 
      message:@"Take Tablets" 
      delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 
    } 



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
    { 
     NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; 

     if([title isEqualToString:@"ok"]) 
     { 
      if(timer) 
      { 
       [timer invalidate]; 
       timer=nil; 
      } 
     } 
    } 

回答

0

startTimer,你可以检查,如果计时器已经在运行,启动一个新的前失效了。

if (timer) [timer invalidate]; 
timer = nil; 
timer = [NSTimer scheduledTimerWithTimeInterval:....]; 
+0

感谢花花公子....嘿u能帮助..hey当用户按下按钮,然后另一个图像应该对图像的顶部和移除时再次用户presses..please帮我.. – sharanu

+0

至于按钮的问题,只需将'@ selector'动作分配给按钮,将'UIImageView'的'hidden'属性设置为YES或NO。你可以用'@ selector'操作来覆盖'UIImageView'和一个不可见的'UIButton'(类型为'UIButtonTypeCustom')来隐藏它。 – Mundi