iOS 表视图中的计时器

iOS Timer in tableview

我有多个单元格的表格视图,每个单元格都有一个按钮, 单击该按钮时,该按钮将被禁用,计时器开始计时 30 秒。 30 秒后按钮应再次启用。按钮下方有标签,应该倒计时按钮再次启用的秒数。

1- 问题是当用户滚动按钮时再次启用,因为单元格是可重复使用的。 如何避免这种情况?

2- 如何显示该单元格的剩余时间? .. 可重复使用的电池再次导致问题

代码示例..

/// Table View Code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"cell1";
    static NSString *simple2TableIdentifier = @"cell2";


    Cell1 *cell = (Cell1 *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    Cell2 *cell2 = (Cell2 *)[tableView dequeueReusableCellWithIdentifier:simple2TableIdentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"xxxxxx" owner:self options:nil];
        cell = [nib objectAtIndex:0];

    }
    if (cell2 == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"yyyyyyyyy" owner:self options:nil];
        cell = [nib objectAtIndex:0];

    }

    if(![[[self.info objectAtIndex:indexPath.row]objectForKey:@"someKeyGoesHere"]isEqualToString:@""])
    {
        cell.some_id.text=[[self.info objectAtIndex:indexPath.row] objectForKey:@"order_id"];

        ///////////////////////////////////////////////
        if ([[[self.info objectAtIndex:indexPath.row] objectForKey:ccc] isEqualToString:@"2"] && ([tempo isEqualToString:uu] || [tempo isEqualToString:bbb]) )
        {
            cell.btn.hidden=NO;
            cell.btn.tag=[[[self.info objectAtIndex:indexPath.row] objectForKey:@"some_id"] integerValue];
            [cell.btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        }
        else
        {
            cell.btn.hidden=YES;
        }

        if(!cell.btn.isHidden)
        {
            if([[Manager btns]containsObject:cell.btn])
            {
                cell.btn.enabled=NO;
            }
            else
            {
                cell.btn.enabled=YES;
            }
        }
        ///////////////////////////////////////////////


        return cell;

    }
    else
    {
        ///some code
        return cell2;
    }


}


-(void)btnClicked:(UIButton*)sender
{
    NSLog(@"A: %li",(long)sender.tag);
    [Manager startTimer:sender];

}


//Sigleton Code

-(void)startTimer:(UIButton*)btn
{
    NSLog(@"start");
    btn.enabled=NO;
    [btns addObject:btn];
    NSLog(@"O_O: %li",(long)btn.tag);
    [NSTimer scheduledTimerWithTimeInterval:10
                                     target:self
                                   selector:@selector(stopTimer:)
                                   userInfo:nil
                                    repeats:NO];
}
-(void)stopTimer:(NSTimer *)timer
{

    NSLog(@"stop");
    [timer invalidate];
    ((UIButton *)[btns firstObject]).enabled=YES;
    NSLog(@"O_O: %li",(long)((UIButton *)[btns firstObject]).tag);
    [btns removeObject:[btns firstObject]];

}

提前致谢。

你必须这样考虑 - 我的模型完成了工作,单元格只显示了正在发生的事情

因此,您在 UITableViewCell 范围之外添加计时器,在 cellForRow 中,您将 "only" 引用您的建模并相应地调整单元格 (enabled/disabled)。如果你想让它倒计时(~ 显示实时倒计时),你需要触发 NSNotification 并用单元格视图捕捉它让它倒计时。或者,总有一些框架,例如 ReactiveCocoa or SwiftBond,您可以在其中将视图绑定到特定逻辑,然后它们会对此做出反应。

如果您不将其分开,您最终会得到您所描述的结果 - 可重复使用的单元会自行重置。