TableView 中的 iOS 计时器

标签 ios objective-c nstimer uitableview

我有多个单元格的表格 View ,每个单元格都有一个按钮, 单击该按钮时,该按钮将被禁用,计时器开始计时 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 中您将“仅” 引用您的模型并相应地调整单元格(启用/禁用)。如果你想让它倒计时(~显示实时倒计时),你需要触发 NSNotification 并用单元格 View 捕捉它以让它倒计时。或者总是有框架,例如 ReactiveCocoaSwiftBond您将观点与特定逻辑联系起来,然后它们会对此使用react。

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

关于 TableView 中的 iOS 计时器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32244759/

相关文章:

ios - 剪辑以限制 subview 内的特定 View

iphone - 如何设计带有tableview内容的可扩展 View ?

ios - 如何实现一个动画队列,一个动画又一个动画结束

ios - 内部使用 self 的队列调用方法上的 weakSelf

ios - UNNotificationRequest 每天在特定时间发送本地通知

objective-c - 请解释 Objective C 中的 Getter 和 Setter

iOS:如何从地标获取邮​​政编码

ios - 具有单独 timeleft 的多个 NSTimer

multithreading - Swift:不断从服务器请求数据的最佳方式是什么

ios - 快速计时器应用程序