iOS之TableViewCell上的倒計時

電商上很多需求都有倒計時功能。如果每個cell上加個定時器,性能上不好,所以這個問題不太好解決。借鑒了下別人思路寫了了demo。

思路:

  1. 首先在控制器里設置一個定時器,就一個定時器就夠了。
  2. 在定時器的執行方法里。把所有的后臺返回的秒數減一。這是控制數據源。等著下次滑動的時候秒數就減少了。然后發通知給cell。讓cell里的顯示秒數的label變化。

上代碼TimeCell

#import <UIKit/UIKit.h>

#define TimeCellNotification  @"NotificationTimeCell"

@interface TimeCell : UITableViewCell

//作用是只是刷新顯示在屏幕上的時間
@property(nonatomic,assign)BOOL isDisplay;

- (void)setSecond:(NSNumber *)second row:(NSInteger) row;

@end

#import "TimeCell.h"

@interface TimeCell ()

@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UILabel *timeLabel;
@property(nonatomic,strong)NSNumber *time;
@property(nonatomic,assign)NSInteger row;

@end

@implementation TimeCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(notificationCenterEvent)
                                                     name:TimeCellNotification
                                                   object:nil];
        [self loadUI];
        
    }
    return self;
}

- (void)loadUI
{
  
    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.textColor = [UIColor blackColor];
    [self.contentView addSubview:self.titleLabel];
    
    self.timeLabel = [[UILabel alloc]init];
    self.timeLabel.textAlignment = NSTextAlignmentCenter;
    self.timeLabel.textColor = [UIColor blackColor];
    [self.contentView addSubview:self.timeLabel];
    
    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.titleLabel.frame = CGRectMake(0, 0, 100, 50);
    self.timeLabel.frame = CGRectMake(self.frame.size.width-100, 0, 100, 50);
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}



- (void)setSecond:(NSNumber *)second row:(NSInteger) row
{
    self.time = second;
    self.row = row;
    self.timeLabel.text = [self currentTimeString];
    self.titleLabel.text = [NSString stringWithFormat:@"%ld",row];
}

- (NSString*)currentTimeString {
    
    NSInteger second = [self.time integerValue];
    if (second <= 0) {
        
        return @"00:00:00";
        
    } else {
        
        return [NSString stringWithFormat:@"%02ld:%02ld:%02ld",(long)second/3600,(long)second%3600/60,(long)second%60];
    }
}

- (void)notificationCenterEvent{
    
    if (self.isDisplay) {
        self.time = @([self.time integerValue] - 1);
        [self setSecond:self.time row:self.row];
    }
}

@end


控制器代碼

#import "ViewController.h"
#import "TimeCell.h"

static NSString *cellID = @"cellID";

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)NSTimer *timer;

@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataArray = [NSMutableArray array];
    
    for (int i=0; i<30; i++) {
        
        NSInteger second = 100 + arc4random_uniform(1000);
        [self.dataArray addObject:@(second)];
        
    }
    
    self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[TimeCell class] forCellReuseIdentifier:cellID];
    [self.view addSubview:self.tableView];
    
    self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TimeCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    [cell setSecond:self.dataArray[indexPath.row] row:indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    TimeCell *timeCell = (TimeCell *)cell;
    timeCell.isDisplay = YES;
    [timeCell setSecond:self.dataArray[indexPath.row] row:indexPath.row];
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath
{
    TimeCell *timeCell = (TimeCell *)cell;
    timeCell.isDisplay = NO;
    
}

- (void)timeAction
{
    for (int i=0; i<self.dataArray.count; i++) {
        NSInteger second = [self.dataArray[i] integerValue];
        second = second - 1;
        self.dataArray[i] = @(second);
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:TimeCellNotification object:nil];
}

@end

得到的.gif

demo地址https://github.com/yinbowang/tableviewcellTimerDemo

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,200評論 4 61
  • 我們都知道PHP是單進程執行的,PHP處理多并發主要是依賴服務器或PHP-FPM的多進程及它們進程的復用,但PHP...
    北山學者閱讀 557評論 0 3
  • 最近,項目上需要一個能 24 小時運行的桌面版操作系統,像 Raspberry Pi、Radxa 這樣的板子,簡單...
    ITJason閱讀 245評論 0 0