从UITableViewCell按钮设置计时器

时间:2011-08-13 08:41:38

标签: iphone

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

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

1 个答案:

答案 0 :(得分:0)

startTimer中,您可以检查计时器是否已在运行并在启动新计时器之前使其无效。

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