iOS cell上的定時器
轉載地址:http://blog.csdn.net/littleSun_zheng/article/details/51426859
實現原理:
1、將資料(資料中的時間部分),以字典(字典中包括:記錄所存資料下標,還有所剩時間 兩個 key)的形式儲存到陣列中。
2、開啟定時器,執行:遍歷整個陣列,每秒減 一,然後儲存到他原來的位置。
3、每次減一之後,根據之前記錄的下標找到對應的Cell,然後修改上面的數。這樣就可以了。
提示:分組型別的tableView 也是沒有一點問題的。效果如APP“E中醫”裡面的名醫出診功能。
簡單的一個demo:
command + a 程式碼(直接貼上到剛剛新建工程的ViewController.m檔案中即可):
<code class="hljs objectivec has-numbering" style="display: block; padding: 0px; background-color: transparent; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// ViewController.m</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// CountdownDemo</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Created by 鄭鵬 on 16/5/16.</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Copyright © 2016年 littleSun_zheng. All rights reserved.</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//</span> <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#import <span class="hljs-title" style="box-sizing: border-box;">"ViewController.h"</span></span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@interface</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">ViewController</span> ()<<span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">UITableViewDataSource</span>>{</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//存放假資料的陣列</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableArray</span> *_textArry; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//時間倒數計時的 時間陣列</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableArray</span> *_totalLastTime; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//定時器</span> NSTimer *_timer; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//記錄 indexPath</span> <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> _num; } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@property</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">nonatomic</span>,<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">strong</span>)<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UITableView</span> *tableView; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@end</span> <span class="hljs-class" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@implementation</span> <span class="hljs-title" style="box-sizing: border-box; color: rgb(102, 0, 102);">ViewController</span></span> - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)viewDidLoad { [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span> viewDidLoad]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Do any additional setup after loading the view, typically from a nib.</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//模擬倒數計時 資料</span> <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//模擬所用資料</span> [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> _modelData]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//初始化 tableView</span> [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> _creatTableView]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//處理資料 並 在主執行緒 開啟定時器</span> [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> _operateData]; } <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#pragma mark--假資料</span> - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)_modelData{ _totalLastTime = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableArray</span> array]; _textArry = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSMutableArray</span> array]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">101</span>; i++) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (i%<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">10</span> == <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>) { [_textArry addObject:[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> numberWithInt:i]]; } } } <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#pragma mark--初始化標檢視</span> - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)_creatTableView{ _tableView = [[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UITableView</span> alloc] initWithFrame:CGRectMake(<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>,<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">64</span>, [UIScreen mainScreen]<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.bounds</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.size</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.width</span> ,[UIScreen mainScreen]<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.bounds</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.size</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.height</span> - <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">64</span>) style:UITableViewStylePlain]; _tableView<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.dataSource</span> = <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span>; _tableView<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.rowHeight</span> = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">44</span>; [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.view</span> addSubview:_tableView]; } <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#pragma mark -- 處理資料 並 在主執行緒 開啟定時器</span> - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)_operateData{ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (_textArry<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.count</span> != <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>) { <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i=<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i<_textArry<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.count</span>; i++) { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSNumber</span> *allSecond = _textArry[i]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSInteger</span> remainIntSecond = [allSecond integerValue]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> *dic = @{@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"indexPath"</span>:[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> stringWithFormat:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%i"</span>,i],@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"lastTime"</span>: [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> stringWithFormat:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%ld"</span>,(<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">long</span>)remainIntSecond]}; [_totalLastTime addObject:dic]; } } <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">dispatch_async</span>(dispatch_get_main_queue(), ^{ <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//開啟定時器</span> [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> _startTimer]; [_tableView reloadData]; }); } <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#pragma mark--開啟定時器 時處理</span> - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)_startTimer{ _timer = [NSTimer scheduledTimerWithTimeInterval:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1.0</span> target:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">self</span> selector:<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@selector</span>(refreshLessTime) userInfo:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span> repeats:<span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">YES</span>]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">//如果不新增下面這條語句,在UITableView拖動的時候,會阻塞定時器的呼叫(為什麼我還不怎麼明白,希望能相互交流)</span> [[NSRunLoop currentRunLoop] addTimer:_timer forMode:UITrackingRunLoopMode]; } - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)refreshLessTime{ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> time; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">for</span> (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">int</span> i = <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>; i < _totalLastTime<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.count</span>; i++) { time = [[[_totalLastTime objectAtIndex:i] objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"lastTime"</span>] intValue]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (time > <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>) { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSIndexPath</span> *indexPath = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSIndexPath</span> indexPathForRow:[[[_totalLastTime objectAtIndex:i] objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"indexPath"</span>] integerValue] inSection:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UITableViewCell</span> *cell = [_tableView cellForRowAtIndexPath:indexPath]; cell<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.textLabel</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.text</span> = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> stringWithFormat:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%i"</span>, time]; time = time - <span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSDictionary</span> *dic = @{@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"indexPath"</span>: [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> stringWithFormat:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%d"</span>,i],@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"lastTime"</span>: [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> stringWithFormat:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"%i"</span>,time]}; [_totalLastTime replaceObjectAtIndex:i withObject:dic]; }<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">else</span> { <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSIndexPath</span> *indexPath = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSIndexPath</span> indexPathForRow:[[[_totalLastTime objectAtIndex:i] objectForKey:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"indexPath"</span>] integerValue] inSection:<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>]; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UITableViewCell</span> *cell = [_tableView cellForRowAtIndexPath:indexPath]; cell<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.textLabel</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.text</span> = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> stringWithFormat:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"littleSun倒數計時結束"</span>]; } } } <span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#pragma mark--UITableViewDataSource</span> - (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSInteger</span>)tableView:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UITableView</span> *)tableView numberOfRowsInSection:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSInteger</span>)section{ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> _totalLastTime<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.count</span>; } - (<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UITableViewCell</span> *)tableView:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UITableView</span> *)tableView cellForRowAtIndexPath:(<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSIndexPath</span> *)indexPath{ <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">static</span> <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *countdownCell = @<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"CountdownDemo_Cell"</span>; <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UITableViewCell</span> *cell = [tableView dequeueReusableCellWithIdentifier:countdownCell]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">if</span> (cell == <span class="hljs-literal" style="color: rgb(0, 102, 102); box-sizing: border-box;">nil</span>) { cell= [[<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">UITableViewCell</span> alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:countdownCell]; } <span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> *remainSecond = _textArry[indexPath<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.row</span>]; cell<span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.textLabel</span><span class="hljs-variable" style="color: rgb(102, 0, 102); box-sizing: border-box;">.text</span> = [<span class="hljs-built_in" style="color: rgb(102, 0, 102); box-sizing: border-box;">NSString</span> stringWithFormat:@<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"還剩下:%@ 秒"</span>,remainSecond]; <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> cell; } - (<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">void</span>)didReceiveMemoryWarning { [<span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">super</span> didReceiveMemoryWarning]; <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">// Dispose of any resources that can be recreated.</span> } <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">@end</span></code>
相關文章
- Swift iOS : 定製CellSwiftiOS
- iOS定時器iOS定時器
- [iOS]各種定時器–最全的定時器使用iOS定時器
- iOS中的3種定時器iOS定時器
- iOS 動畫-定時器動畫iOS動畫定時器
- iOS 中 cell的用法iOS
- iOS 定時器耗電探究iOS定時器
- BIOS相關定時器iOS定時器
- cell (iOS表格) - 簡單實現一個定時器管理600個倒數計時 - 2.程式碼介紹篇iOS定時器
- iOS 重寫cell的FrameiOS
- ios 多執行緒定時器iOS執行緒定時器
- iOS 點選UITableViewCell上的控制元件獲取對應的celliOSUIView控制元件
- iOS cell找對應的tableViewiOSView
- Swift iOS : self sized cellSwiftiOSZed
- IOS 動態改變cell的高度iOS
- iOS之移動中變化的cell(cell逐漸變大效果)iOS
- 定時器以及定時器的幾個案例定時器
- IOS多型別Cell的tableView實現iOS多型型別View
- 51定時器/計數器齊上陣,借鑑定時器
- Go 的定時器Go定時器
- iOS UITableView Cell和 SectionHeader 的呼叫順序iOSUIViewHeader
- MUI TableViewCell 上按鈕點選Cell同時觸發解決UIView
- 定時器定時器
- Cell定製和Frame模型引入模型
- (iOS)點選cell推出現新介面iOS
- iOS cell中webview自適應高度iOSWebView
- iOS 實現展開TableViewCell,下拉celliOSView
- 定時備份windows機器上的檔案到linux伺服器上的操作梳理(rsync)WindowsLinux伺服器
- 定時關閉AWS上的EC2機器例項
- java定時器的使用Java定時器
- iOS面試題:tableivew上面展示圖片的時候,在網路還未回來時,將cell滑走,那被划走的cell還會不會顯示圖片?iOS面試題
- iOS 自定義tableView Cell、高度自適應iOSView
- JavaScript定時器JavaScript定時器
- js定時器JS定時器
- JavaScript 定時器JavaScript定時器
- 核心定時器定時器
- 利用定時器實現51微控制器返回上電執行時間功能定時器
- 【JavaScript定時器小案例】常見的幾種定時器實現的案例JavaScript定時器