动态显示UITableViewCell中的StopWatch Timer [英] Display StopWatch Timer in UITableViewCell dynamically

查看:146
本文介绍了动态显示UITableViewCell中的StopWatch Timer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保留计时器值并从新的 UITableViewCell 开始显示它但我的问题是我能够成功地在第一个Cell上显示秒表计时器但是当我试图在 UITableView 中添加一个新单元格所以我的计时器设置为第二个单元格,我无法定义如何让我的第一个计时器保持活动状态。

I want to hold timer values and Display it from start in new UITableViewCell but my problem is that I am successfully able to display stopwatch timer on first Cell but when I am trying to add a new cell in UITableView so my timer is set to second cell and I am unable to define how to keep my first timer alive.

我想过管理一个数组,但我想每次调用reload TableData,但我认为这很糟糕:(

I thought of managing an Array but I want to call reload TableData everytime, but I think that is bad :(

我试过解决如下所示,但我不知道如何在前一个单元格上显示timerValue,使用相同计数的秒表动态和从开始的新单元格timerValue。

I tried work around as below but I am not figuring out how to show timerValue on previous cell with working stopwatch dynamic from same count and new cell timerValue from start.

静态解决方案,但我的行正在动态插入并从TableView中删除,因此静态解决方案限制了我特定的行数,并寻找动态解决方案。

A static solution but my rows are inserting dynamically and deleting aswell from TableView so static solution limits me for specific count of rows, and looking for dynamic solution.

    - (void) putDelayOnTimeOut {


            if (!_timerQueue) {
                _timerQueue = dispatch_queue_create("timer_queue", NULL);
            }
            dispatch_async(_timerQueue, ^{

                self.startTime = [NSDate date];

                if (globalRowIndex == 0) {

                    _timer = [NSTimer scheduledTimerWithTimeInterval:0.6 target:self selector:@selector(showTimeoutActivity:) userInfo:nil repeats:YES];
                } else if (globalRowIndex == 1) {
                    _timer = [NSTimer scheduledTimerWithTimeInterval:0.6 target:self selector:@selector(showTimeoutActivity1:) userInfo:nil repeats:YES];
                }

                //[[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
                [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];

                //[[NSRunLoop currentRunLoop] run];
                [_timer fire];
            });

    }


    - (void)showTimeoutActivity:(id)unused
    {
        if( [_timer isValid] )
        {
            NSTimeInterval interval = [self.startTime timeIntervalSinceNow];
            interval = (-1 * interval);

            NSString *timerString = [self formatInterval:interval];
            NSLog(@"%@", timerString);

            MyCell *cell = (MyCell *)[self.activeCallTbl viewWithTag:globalRowIndex+10];
            //NSIndexPath *indexPath = [self.activeCallTbl indexPathForCell:cell];
            //NSLog(@"%d",indexPath.row);
            cell.timerLbl.text = timerString;

        }
    }


 - (void)showTimeoutActivity1:(id)unused
    {
        if( [_timer isValid] )
        {
            NSTimeInterval interval = [self.startTime timeIntervalSinceNow];
            interval = (-1 * interval);


            NSString *timerString = [self formatInterval:interval];
            NSLog(@"%@", timerString);

            MyCell *cell = (MyCell *)[self.activeCallTbl viewWithTag:globalRowIndex+10];
            //NSIndexPath *indexPath = [self.activeCallTbl indexPathForCell:cell];
            //NSLog(@"%d",indexPath.row);
            cell.timerLbl.text = timerString;

        }
    }


推荐答案

我不确定是否完全理解了你的要求......但我尝试了。

I'm not sure to have understood what you are asking for exactly...but I try.

一个简单的解决方案可以是你的UITableViewCell的子类并添加细胞本身的计时器。因此,单元格将具有对计时器的引用,并且每次将新单元格添加到TableView时都会创建一个新单元格。

One simple solution can be to subclass your UITableViewCell and add the timer to the cell itself. So the cell will have the reference to the timer and will create a new one every time a new cell is added to the TableView

我附加示例代码:链接(我已静态添加3个单元格,但您可以根据需要添加,只需点击一个单元格启动计时器,如果再次点击,计时器将从0开始重新启动。

I attach example code: Link (I've added 3 cells statically, but you can add as you want, simply tap on one cell to start the timer, if you tap again the timer starts again from 0)

创建自定义单元子类并添加如下方法:

Create your custom cell subclass and add some methods like these:

- (void) startTimer
{

    // invalidate a previous timer in case of reuse
    if (self.timer)
        [self.timer invalidate];


    self.startTime = [NSDate date];

    // create a new timer
    self.timer = [NSTimer scheduledTimerWithTimeInterval:0.6 target:self selector:@selector(calculateTimer) userInfo:nil repeats:YES];

    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];

    [self.timer fire];
}

- (void)calculateTimer
{
    NSTimeInterval interval = [self.startTime timeIntervalSinceNow];
    interval = (-1 * interval);

    NSString *intervalString = [NSString stringWithFormat:@"%f", interval];

    self.timerLabel.text = intervalString;
}

然后,在UITableViewController实现中,只需调用

Then, in the UITableViewController implementation, simply call

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    TimerCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    [cell startTimer];
}

有关详细信息,请查询或查看附件项目。

For more details ask or look at the attached project.

这是我发现的最快的方式,虽然有人宁愿在表视图控制器中留下定时器代码和引用(但是你需要更多的代码)

This is the quickest way I've found, although someone would prefer to leave the timer code and reference inside the Table View Controller (But you'll need more code)

编辑

EDIT

正如Spynet在评论中写的那样赢了'如果您有超过7/8个单元格(可以适合屏幕的单元格数),则可以正常工作,因为当您滚动UITableView单元格时会被重复使用。因此,单个单元格将用于不同的indexPaths,因此在第一个单元格上启动计时器,然后滚动,将使您看到计时器已经每7/8个单元格启动(因为它是先前创建的单元格的计时器)

As written in the comment by Spynet this won't work if you have more than 7/8 cells (the number of cells that can fit the screen), because when you scroll the UITableView cells are reused. So a single cell will be used in different indexPaths, so starting a timer on the first cell, then scrolling, will cause you to see the timer already started every 7/8 cells (because it's the timer of the previously created cell)

要解决这个问题,有很多方法。一种可能是从单元格中删除计时器并在ViewController中创建一组计时器。每个索引路径一个计时器。
另一种方式(但是如果单元格很多,我就不重复它)是不重用单元格。

To solve this there are many ways. One could be to remove the timer from the cell and create an array of timers in the ViewController. One timer for each indexpath. Another way (but I don't recomed it if the cells are many) is to don't reuse cells.

从我的示例项目开始:< br>
1 - 创建一个nib文件(称之为TimerCell.xib)并从故事板中复制/粘贴单元原型

2 - 将此属性添加到 TimerViewController

Starting from my Example Project:
1 - create a nib file (call it TimerCell.xib) and copy/paste the cell prototype inside it from the storyboard
2 - add this property to the TimerViewController

@interface TimerViewController ()
@property (strong, nonatomic) NSMutableArray *cells;
@end

3 - initWithCoder 方法初始化数组:

- (id)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        _cells = [NSMutableArray array];
    }
    return self;
}

4 - 更新 cellForRowAtIndexPath 方法并将其更改为:

4 - update the cellForRowAtIndexPath method and change it like this:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    TimerCell *cell = nil;

    if ([self.cells count] > indexPath.row)
    {
        cell = [self.cells objectAtIndex:indexPath.row];
    } else {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"TimerCell" owner:self options:nil] objectAtIndex:0];
        [self.cells addObject:cell];
    }

    // Configure the cell...

    return cell;
}

通过这种方式,单元格不会被重复使用

In this way the cells won't be reused

这篇关于动态显示UITableViewCell中的StopWatch Timer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆