【问题标题】:how to use timer in uitableview?如何在uitableview中使用计时器?
【发布时间】:2017-04-20 07:54:17
【问题描述】:

我得到毫秒,我想将该毫秒转换为天、小时、分钟、秒并显示并希望在 uiTableview 中显示它。我想每秒更新一次那个计时器..

我曾使用 uiTableview 委托方法 cellForRowAtIndexPath 尝试过这段代码。

NSInteger totleTime = [[[productListDataArray objectAtIndex:indexPath.row]objectForKey:@"_auction_duration"] integerValue];


         totleTime =  totleTime - timerValue ;


         long days = (totleTime) / (60  * 60 * 24);
         long hours = (totleTime - days  * 60 * 60 * 24) / (60 * 60);
         long minuts = (totleTime - days * 60 * 60  * 24 - hours * 60 * 60) / 60;
         long seconds = (totleTime - days * 60 * 60 * 24 - hours * 60  *60 - minuts * 60);

 NSLog(@"seconds : %.f minutes : %.f hours : %.f days : %.f ", seconds, minuts, hours, days);

我曾经用这个方法来调用方法和更新tableview。但它不起作用所以建议我另一种选择或告诉我我在这段代码中做错了什么..

【问题讨论】:

    标签: uitableview nstimer countdown nstimeinterval


    【解决方案1】:

    我从你的问题中了解到,你想每秒重新加载你的 tableView。首先,让我说这是一个坏主意。如果您的 tableView 正在重新加载,那么用户将无法看到任何内容。话虽如此,您可以使用具有特定间隔的计时器来调用方法。该方法可以决定何时重新加载 tableView。

    编辑

    根据您的上一条评论,我了解到您正试图在表格视图单元格中显示倒数计时器。为此,您无需重新加载 tableview。您只需更新数据源并重新加载显示计数器的行。类似的东西。

    下面的代码 sn-p 在 Swift 2 中

    //Call a method named tick() every second
    let timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(tick), userInfo: nil, repeats: true)
    
    func tick() {
    
        //DO YOUR CALCULATIONS HERE TO CALCULATE YOUR DAYS, MINUTES, etc. THEN CALL THE FOLLOWING METHOD
    
        tblView.reloadRowsAtIndexPaths([NSIndexPath(forRow: {rowNumber}, inSection: {sectionNumber})], withRowAnimation: .None)
    
        //ALSO MAKE SURE YOUR cellForRowAtIndexPath IS CORRECTLY SETTING THE CELL VALUES
    }
    

    完成后别忘了invalidate计时器

    【讨论】:

    • 我试过这个但没有得到正确的输出
    • 你能解释一下你到底尝试了什么,预期输出是什么,实际输出是什么?
    • 我可以得到毫秒:- 5426977 并且输出是 blove :- 秒:37 分钟:29 小时:19 天:62 但现在可以显示用户 1. 62:19:29:37 2。 62:19:29:36 3. 62:19:29:35
    • 问题需要明确且切中要害。你没有给我提供太多的东西。我不知道 5426977 在这种情况下意味着什么。请确保您的问题有据可查并提及所有相关细节。
    • totleTime= 5426977;每秒调用方法并重新加载表和脉冲值。 timerValue += 1000
    【解决方案2】:

    我已经解决了这个问题。

    1.最初我设置的计时器是 1 秒。

      [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateCounter:) userInfo:nil repeats:YES];
    

    2.Every Second Call Method 并更改值并重新加载tableview。

    - (void)updateCounter:(NSTimer *)theTimer {
         timerValue += 1;
        [ProductListTableView reloadData];
    }
    

    3。现在在uitableView deleget方法中计算剩余时间cellForRowAtIndexPath

    NSInteger totleTime = [[[productListDataArray objectAtIndex:indexPath.row]objectForKey:@"_auction_duration"] integerValue];
    
    
             totleTime =  totleTime - timerValue ;
    
    
             long days = (totleTime) / (60  * 60 * 24);
             long hours = (totleTime - (days * 60 * 60 * 24)) / (60 * 60) ;
    
             long minuts = (totleTime - (days * 60 * 60  * 24) - (hours * 60 * 60)) / 60;
             long seconds = (totleTime - (days * 60 * 60 * 24) - (hours * 60 * 60) - (minuts * 60));
    
    
    
            cell.saleTimeLabel.text = [NSString stringWithFormat:@"%@ Sold %ld:%ld:%ld:%ld",[[productListDataArray objectAtIndex:indexPath.row]objectForKey:@"sales_qty"], days,hours,minuts,seconds];
    

    【讨论】:

      【解决方案3】:

      如果 timeValue 假设系统时间,我不知道 timeValue 的含义。你可以写一个定时器来重新加载tableView的数据。

      【讨论】:

        猜你喜欢
        • 2021-10-28
        • 2019-05-18
        • 2021-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多