2011-08-13 35 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