iOS倒计时秒杀界面 tableview

miaoShaTest.gif

根据时间数组和cell id数组 获取每个cell的倒计时时间 并在cell显示时赋值


- (void)getTimeStrWith:(NSArray *)timeStrArr and:(NSArray *)cellIdArr{
    
    dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);
    for (int i = 0; i< timeStrArr.count; i++) {
        
        __block int timeout=[timeStrArr[i] intValue]; //倒计时时间
        
        dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
        dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
        
        dispatch_source_set_event_handler(_timer, ^{
            
            if(timeout<=0){ //倒计时结束,关闭
                dispatch_source_cancel(_timer);
                dispatch_async(dispatch_get_main_queue(), ^{
                    [_dataDic setObject:@"停止秒杀"  forKey:cellIdArr[i]];
                    [_myTableView reloadData];
                });
            }else{
                int minutes = timeout / 60;
                int seconds = timeout % 60;
                NSString *strTime = [NSString stringWithFormat:@"%d分%.2d秒后停止秒杀",minutes, seconds];
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [_dataDic setObject:strTime forKey:cellIdArr[i]];
                   
                    [_myTableView reloadData];
                });
                timeout--;
            }
            
        });
        dispatch_resume(_timer);
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JQTableViewCell *cell = [JQTableViewCell initWith:tableView];   
    cell.cellID = _cellidArr[indexPath.row];   
    cell.timeLineLabel.text = [_dataDic objectForKey:cell.cellID];
 
    return cell;
}

鉴于真机测试会发生后台timer不运行的情况,增加一个程序进入后台通知,延长后台任务执行

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForegroundNotification) name:UIApplicationDidEnterBackgroundNotification object:nil];

- (void) appWillEnterForegroundNotification{
    UIApplication*   app = [UIApplication sharedApplication];
    __block    UIBackgroundTaskIdentifier bgTask;
    //申请一个后台执行的任务 大概10分钟 如果时间更长的话需要借助默认音频等
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        
        dispatch_async(dispatch_get_main_queue(), ^{
            if (bgTask != UIBackgroundTaskInvalid)
            {
                bgTask = UIBackgroundTaskInvalid;
            }
        });
    }];

}

Demo地址 欢迎Star

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,650评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,169评论 25 709
  • 一、农牧业:农艺师、畜牧师、兽医师 二、医药工程:医学、药品、医疗器械、制药机械、药用包装 三、水产工程: 加工、...
    编辑小安阅读 4,938评论 0 1
  • 【你把跑步当成一件快乐的事情了吗?】 你需要在一个深夜寂静的时刻进行一个辩证的思考,让身体处于一个静能生慧的状态,...
    贺小桶阅读 1,459评论 0 1
  • 稍稍一晃立夏了,也不管有多少任务没有完成,时间的大脚一大步又一大步碾压过来。 但是立夏后,有房檐下的萤火虫一闪一闪...
    又新阅读 4,028评论 0 2

友情链接更多精彩内容