SVPullToRefresh開源庫地址
https://github.com/samvermette/SVPullToRefresh
將整個文件夾SVPullToRefresh拖入工程中並引入頭文件即可
注意編譯時有一個方法快被棄用了
- (CGSize)sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode
工程源碼
RootViewController.h
1 // Copyright (c) 2014年 YouXian. All rights reserved. 2 // 3 4 #import <UIKit/UIKit.h> 5 6 @interface RootViewController : UIViewController 7 8 @end
RootViewController.m
1 // Copyright (c) 2014年 YouXian. All rights reserved. 2 // 3 4 #import "RootViewController.h" 5 #import "SVPullToRefresh.h" 6 7 @interface RootViewController () <UITableViewDelegate, UITableViewDataSource> 8 9 @property (nonatomic, strong) UITableView *tableView; 10 @property (nonatomic, strong) NSMutableArray *dataSource; 11 12 @end 13 14 @implementation RootViewController 15 16 - (void)viewDidLoad 17 { 18 [super viewDidLoad]; 19 20 //初始化 tableView 21 _tableView = [[UITableView alloc] initWithFrame:self.view.bounds 22 style:UITableViewStyleGrouped]; 23 _tableView.delegate = self; 24 _tableView.dataSource = self; 25 [self.view addSubview:_tableView]; 26 27 //初始化數據源 28 _dataSource = [[NSMutableArray alloc] init]; 29 for (int i = 0; i < 10; i++) 30 { 31 [_dataSource addObject:[NSString stringWithFormat:@"%@", [NSDate date].description]]; 32 } 33 34 //注冊下拉刷新功能 35 __weak RootViewController *weakSelf = self; 36 [_tableView addPullToRefreshWithActionHandler:^{ 37 [weakSelf insertRowAtTop]; 38 }]; 39 40 //注冊上拉刷新功能 41 [_tableView addInfiniteScrollingWithActionHandler:^{ 42 [weakSelf insertRowAtBottom]; 43 }]; 44 } 45 46 #pragma mark - 47 #pragma mark PullToRefreshInsertRow 48 49 - (void)insertRowAtTop 50 { 51 int64_t delayInSeconds = 2.0; 52 dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); 53 dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 54 //開始更新 55 [_tableView beginUpdates]; 56 57 //插入數據到數據源(數組的開頭) 58 [_dataSource insertObject:[NSString stringWithFormat:@"%@", [NSDate date].description] 59 atIndex:0]; 60 61 //在tableView中插入一行(Row開頭) 62 [_tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 63 inSection:0]] 64 withRowAnimation:UITableViewRowAnimationBottom]; 65 66 //結束更新 67 [_tableView endUpdates]; 68 69 //停止菊花 70 [_tableView.pullToRefreshView stopAnimating]; 71 }); 72 } 73 74 - (void)insertRowAtBottom 75 { 76 int64_t delayInSeconds = 2.0; 77 dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); 78 dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 79 //開始更新 80 [_tableView beginUpdates]; 81 82 //插入數據到數據源(數組的結尾) 83 [_dataSource addObject:[NSString stringWithFormat:@"%@", [NSDate date].description]]; 84 85 86 //在tableView中插入一行(Row結尾) 87 [_tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:_dataSource.count - 1 88 inSection:0]] 89 withRowAnimation:UITableViewRowAnimationBottom]; 90 91 //結束更新 92 [_tableView endUpdates]; 93 94 //停止菊花 95 [_tableView.infiniteScrollingView stopAnimating]; 96 }); 97 } 98 99 #pragma mark - 100 #pragma mark UITableViewDataSource 101 102 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 103 { 104 return 1; 105 } 106 107 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 108 { 109 return _dataSource.count; 110 } 111 112 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 113 static NSString *identifier = @"Cell"; 114 UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:identifier]; 115 116 if (cell == nil) 117 { 118 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 119 reuseIdentifier:identifier]; 120 } 121 122 cell.textLabel.text = _dataSource[indexPath.row]; 123 124 return cell; 125 } 126 127 @end
心得:
使用簡單,邏輯清晰,開源庫使用block實現, RootViewController.m 35行代碼處要將RootViewController自身傳入block中,需要使用弱應用指針,注意.
工程源碼地址:
http://pan.baidu.com/s/1dD24E1V