大佬教程收集整理的这篇文章主要介绍了ios – 在tableview中使用timer会在任何滚动或表重新加载后重新创建计时器,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_801_22@UILabel *lblTimer=(UILabel *)[cell viewWithTag:10]; MZTimerLabel *UpgradeTimer = [[MZTimerLabel alloc] initWithLabel:lblTimer andTimerType:MZTimerLabelTypeTimer]; [UpgradeTimer setCountDownTime:timestamp]; [UpgradeTimer startWithEndingBlock:^(NSTimeInterval @R_944_3118@ { lblTimer.text = @"✔"; }];
但是在任何表重新加载或滚动之后,计时器表现得很奇怪,并且它似乎重新生成多个计时器以便在同一个地方进行计数.
使用此计时器时应该如何解决这个问题?
感谢任何帮助,
埃利亚斯
我会建议抛弃那个课程,并创建自己的课程.实际上很容易实现这样的目标.
>创建一个保存标题和结束日期的新类
>将该类的实例存储在支持表的模型中
>创建一个刷新tableView的NSTimer
>设置你的细胞.
这基本上是表格中基本倒计时所需的所有代码.因为它不会在视图中存储任何数据,所以您可以根据需要滚动:
@H_801_22@@interface Timer : NSObject @property (strong,nonatomiC) NSDate *endDate; @property (strong,nonatomiC) NSString *title; @end @implementation Timer @end @interface MasterViewController () { NSArray *_objects; NSTimer *_refreshTimer; } @end @implementation MasterViewController - (void)viewDidLoad { [super viewDidLoad]; NSMutableArray *modelStore = [NSMutableArray arrayWithCapacity:30]; for (NSInteger i = 0; i < 30; i++) { Timer *timer = [[Timer alloc] init]; timer.endDate = [NSDate dateWithTimeIntervalSinceNow:i*30]; timer.title = [NSString StringWithFormat:@"Timer %ld seconds",(long)i*30]; [modelStore addObject:timer]; } _objects = modelStore; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [_refreshTimer invalidate]; // timer should not exist,but just in case. _refreshTimer = [NSTimer timerWithTimeInterval:0.5f target:self SELEctor:@SELEctor(refreshView:) userInfo:nil repeats:YES]; // should fire while scrolling,so we need to add the timer manually: [[NSRunLoop currentRunLoop] addTimer:_refreshTimer forMode:NSRunLoopCommonModes]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [_refreshTimer invalidate]; _refreshTimer = nil; } - (void)refreshView:(NSTimer *)timer { // only refresh visible cells for (UITableViewCell *cell in [self.tableView visibleCells]) { NSIndexPath *indexPath = [self.tableView indexPathForCell:cell]; [self configureCell:cell forRowATindexPath:indexPath]; } } #pragma mark - Table View - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _objects.count; } - (void)configureCell:(UITableViewCell *)cell forRowATindexPath:(NSIndexPath *)indexPath { Timer *timer = _objects[indexPath.row]; cell.textLabel.text = timer.title; NSInteger timeUntilEnd = (NSInteger)[timer.endDate timeIntervalSinceDate:[NSDate date]]; if (timeUntilEnd <= 0) { cell.detailTextLabel.text = @"Finished"; } else { NSInteger seconds = timeUntilEnd % 60; NSInteger minutes = (timeUntilEnd / 60) % 60; NSInteger hours = (timeUntilEnd / 3600); cell.detailTextLabel.text = [NSString StringWithFormat:@"%02ld:%02ld:%02ld",(long)hours,(long)minutes,(long)seconds]; } } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowATindexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; [self configureCell:cell forRowATindexPath:indexPath]; return cell; } @end
以上是大佬教程为你收集整理的ios – 在tableview中使用timer会在任何滚动或表重新加载后重新创建计时器全部内容,希望文章能够帮你解决ios – 在tableview中使用timer会在任何滚动或表重新加载后重新创建计时器所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。