Swift语言出来之后, 可能还没有第三方的下拉刷新和上提加载, 所以自己用UIRefreshControl控件和UITableView实例的tableFooterView(底部视图)属性结合起来写了一个下拉刷新和点击加载更多的基本实现, 分为OC的代码实现和Swift的代码实现, 希望大家可以指出不足:
Swift代码:
1 import UIKit 2 3 class ViewController: UITableViewController { 4 5 // 用于显示的数据源 6 var _dataSource:[String] = [] 7 8 // 加载更多 状态 风火轮 9 var _aiv:UIActivityIndicatorView! 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 // Do any additional setup after loading the view, typically from a nib. 14 15 // 数据源中的基础数据 16 for i in 0...2 { 17 18 _dataSource.append("\(i)") 19 } 20 21 // 初始下拉刷新控件 22 self.refreshControl = UIRefreshControl() 23 self.refreshControl?.attributedTitle = NSAttributedString(string: "Pull To Refresh") 24 self.refreshControl?.tintColor = UIColor.greenColor() 25 self.refreshControl?.addTarget(self, action: "refresh", forControlEvents: .ValueChanged) 26 27 // 加载更多按扭的背景视图 28 var tableFooterView:UIView = UIView() 29 tableFooterView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 44) 30 tableFooterView.backgroundColor = UIColor.greenColor() 31 self.tableView.tableFooterView = tableFooterView 32 33 // 加载更多的按扭 34 let loadMoreBtn = UIButton() 35 loadMoreBtn.frame = CGRectMake(0, 0, self.view.bounds.width, 44) 36 loadMoreBtn.setTitle("Load More", forState: .Normal) 37 loadMoreBtn.setTitleColor(UIColor.lightGrayColor(), forState: .Normal) 38 loadMoreBtn.addTarget(self, action: "loadMore:", forControlEvents: .TouchUpInside) 39 tableFooterView.addSubview(loadMoreBtn) 40 41 // 加载更多 状态 风火轮 42 _aiv = UIActivityIndicatorView(activityIndicatorStyle: .Gray) 43 _aiv.center = loadMoreBtn.center 44 tableFooterView.addSubview(_aiv) 45 } 46 47 // 加载更多方法 48 func loadMore(sender:UIButton) { 49 50 sender.hidden = true 51 _aiv.startAnimating() 52 53 dispatch_async(dispatch_get_global_queue(0, 0), { () -> Void in 54 55 self._dataSource.append("\(self._dataSource[self._dataSource.count-1].toInt()! + 1)") 56 57 dispatch_async(dispatch_get_main_queue(), { () -> Void in 58 59 sleep(1) 60 61 self._aiv.stopAnimating() 62 sender.hidden = false 63 64 self.tableView.reloadData() 65 }) 66 }) 67 } 68 69 // 下拉刷新方法 70 func refresh() { 71 72 if self.refreshControl?.refreshing == true { 73 74 self.refreshControl?.attributedTitle = NSAttributedString(string: "Loading...") 75 } 76 77 dispatch_async(dispatch_get_global_queue(0, 0), { () -> Void in 78 79 self._dataSource.insert("\(self._dataSource[0].toInt()! - 1)", atIndex: 0) 80 81 dispatch_async(dispatch_get_main_queue(), { () -> Void in 82 83 sleep(1) 84 85 self.refreshControl?.endRefreshing() 86 self.refreshControl?.attributedTitle = NSAttributedString(string: "Pull To Refresh") 87 88 self.tableView.reloadData() 89 }) 90 }) 91 } 92 93 // tableView dataSource 94 override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 95 96 return _dataSource.count 97 } 98 99 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 100 101 let identifier = "cell" 102 103 var cell = tableView .dequeueReusableCellWithIdentifier(identifier) as? UITableViewCell 104 105 if cell == nil { 106 107 cell = UITableViewCell(style: .Default, reuseIdentifier: identifier) 108 } 109 110 cell?.textLabel?.text = "\(_dataSource[indexPath.row])" 111 112 return cell! 113 } 114 }
OC代码:
1 #import "ViewController.h" 2 3 @interface ViewController () 4 { 5 // 数据源 6 NSMutableArray * _dataSource; 7 8 // 风火轮 9 UIActivityIndicatorView * _aiv; 10 } 11 12 @end 13 14 @implementation ViewController 15 16 - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 17 { 18 return self; 19 } 20 21 - (void)viewDidLoad { 22 [super viewDidLoad]; 23 // Do any additional setup after loading the view, typically from a nib. 24 25 // 初始数据源 26 _dataSource = [[NSMutableArray alloc] init]; 27 28 // 基础数据 29 for (int i=0; i<3; i++) { 30 31 [_dataSource addObject:[NSString stringWithFormat:@"%d",i]]; 32 } 33 34 // 刷新控件 35 self.refreshControl = [[UIRefreshControl alloc] init]; 36 self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull To Refresh"]; 37 self.refreshControl.tintColor = [UIColor greenColor]; 38 [self.refreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged]; 39 40 // 背景视图 41 UIView * tableFooterView = [[UIView alloc] init]; 42 tableFooterView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 44); 43 tableFooterView.backgroundColor = [UIColor greenColor]; 44 self.tableView.tableFooterView = tableFooterView; 45 46 // 加载更多按扭 47 UIButton * loadMoreBtn = [[UIButton alloc] init]; 48 loadMoreBtn.frame = CGRectMake(0, 0, self.view.bounds.size.width, 44); 49 [loadMoreBtn setTitle:@"Load More" forState:UIControlStateNormal]; 50 [loadMoreBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal]; 51 [loadMoreBtn addTarget:self action:@selector(loadMore:) forControlEvents:UIControlEventTouchUpInside]; 52 [tableFooterView addSubview:loadMoreBtn]; 53 54 // 风火轮 55 _aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 56 _aiv.center = loadMoreBtn.center; 57 [tableFooterView addSubview:_aiv]; 58 } 59 60 // 加载更多方法 61 - (void)loadMore:(UIButton *)sender 62 { 63 sender.hidden = YES; 64 [_aiv startAnimating]; 65 66 dispatch_async(dispatch_get_global_queue(0, 0), ^{ 67 68 [_dataSource addObject: [NSString stringWithFormat:@"%d", [_dataSource[_dataSource.count-1] intValue] + 1]]; 69 70 dispatch_async(dispatch_get_main_queue(), ^{ 71 72 sleep(1); 73 74 [_aiv stopAnimating]; 75 sender.hidden = NO; 76 77 [self.tableView reloadData]; 78 }); 79 }); 80 } 81 82 // 下拉刷新方法 83 - (void)refresh { 84 85 if (self.refreshControl.refreshing) { 86 87 self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Loading..."]; 88 } 89 90 dispatch_async(dispatch_get_global_queue(0, 0), ^{ 91 92 [_dataSource insertObject:[NSString stringWithFormat:@"%d", [_dataSource[0] intValue] -1] atIndex:0]; 93 94 dispatch_async(dispatch_get_main_queue(), ^{ 95 96 sleep(1); 97 98 [self.refreshControl endRefreshing]; 99 self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull To Refresh"]; 100 101 [self.tableView reloadData]; 102 }); 103 }); 104 } 105 106 // tableView dataSource 107 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 108 109 return _dataSource.count; 110 } 111 112 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 113 114 static NSString * identifier = @"cell"; 115 116 UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 117 118 if (cell == nil) { 119 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 120 } 121 122 cell.textLabel.text = _dataSource[indexPath.row]; 123 124 return cell; 125 } 126 127 @end
时间: 2024-10-07 07:37:53