iOS 下拉刷新和加载更多 (OC\Swift)
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
iOS 下拉刷新和加载更多 (OC\Swift)的更多相关文章
- RecyclerView的下拉刷新和加载更多 动画
下拉刷新和加载更多 1.https://github.com/jianghejie/XRecyclerView 2.http://blog.csdn.net/jabony/article/detail ...
- Android之RecyclerView轻松实现下拉刷新和加载更多
今天研究了下RecyclerView的滑动事件,特别是下拉刷新和加载更多事件,在现在几乎所有的APP显示数据列表时都用到了.自定义RecyclerView下拉刷新和加载更多听上去很复杂,实际上并不难, ...
- 自己封装的工具类,使用原生SwipeRefreshLayout+RecycleView实现下拉刷新和加载更多
实现SwipeRefreshLayout+RecycleView实现刷新 在你的xml文件里写上如下代码: <android.support.v4.widget.SwipeRefreshLayo ...
- RecyclerView 下拉刷新和加载更多
一.SwipeRefreshLayout实现下拉刷新 1.方法API: setOnRefreshListener(OnRefreshListener):添加下拉刷新监听器 setRefreshing( ...
- Android UI--自定义ListView(实现下拉刷新+加载更多)
Android UI--自定义ListView(实现下拉刷新+加载更多) 关于实现ListView下拉刷新和加载更多的实现,我想网上一搜就一堆.不过我就没发现比较实用的,要不就是实现起来太复杂,要不就 ...
- Android Demo 下拉刷新+加载更多+滑动删除
小伙伴们在逛淘宝或者是各种app上,都可以看到这样的功能,下拉刷新和加载更多以及滑动删除,刷新,指刷洗之后使之变新,比喻突破旧的而创造出新的,比如在手机上浏览新闻的时候,使用下拉刷新的功能,我们可以第 ...
- PullToRefresh下拉刷新 加载更多 详解 +示例
常用设置 项目地址:https://github.com/chrisbanes/Android-PullToRefresh a. 设置刷新模式 如果Mode设置成Mode.PULL_FROM_STAR ...
- 分页插件思想:pc加载更多功能和移动端下拉刷新加载数据
感觉一个人玩lol也没意思了,玩会手机,看到这个下拉刷新功能就写了这个demo! 这个demo写的比较随意,咱不能当做插件使用,基本思想是没问题的,要用就自己封装吧! 直接上代码分析下吧! 布局: & ...
- iOS 下拉刷新-上拉加载原理
前言 讲下拉刷新及上拉加载之前先给大家解释UIScrollView的几个属性 contentSize是UIScrollView可以滚动的区域. contentOfinset 苹果官方文档的解释是&qu ...
随机推荐
- Packages
Packages are a way of structuring Python's module namespace by using "dotted module names" ...
- swift系统学习控件篇:UITableView+UICollectionView
工作之余,学习下swift大法.把自己的学习过程分享一下.当中的布局很乱,就表在意这些细节了.直接上代码: UITableView: // // ViewController.swift // UIt ...
- VM设置BIOS延长时间
更改文件中的 bios.bootDelay = "XXXX"
- 批处理中for循环多个%
实例:@echo offset NUM=10000for /f %%i in (字符.txt) do (set JSZF=%%ifor /L %%. in (0,1,%NUM%) do ( Call ...
- WPF文本框密码框添加水印效果
WPF文本框密码框添加水印效果 来源: 阅读:559 时间:2014-12-31 分享: 0 按照惯例,先看下效果 文本框水印 文本框水印相对简单,不需要重写模板,仅仅需要一个VisualBrush ...
- I.MX6 bootargs 内核参数设定
/******************************************************************** * I.MX6 bootargs 内核参数设定 * 说明: ...
- PAT (Basic Level) Practise:1008. 数组元素循环右移问题
[题目连接] 一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN-1)变换为(AN-M …… A ...
- 7-zip的压缩的时候排除某目录
安装暂且不说了. 看一下帮助. [root@localhost Server]# 7z -Zip [] - Igor Pavlov -- p7zip Version ,Utf16=on,HugeFil ...
- SAP 增强说明
转自http://blog.csdn.net/lyb_yt/article/details/8177974 (一)什么是增强(Enhancement)? 简单地说,增强就是ERP系统中标准程序的出口, ...
- HDU 5775 树状数组
Bubble Sort Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...