使用OC和swift创建系统自带的刷新界面
使用OC和swift创建系统自带的刷新界面
一:swift刷新界面代码:
import UIKit
class ViewController: UITableViewController {
// 用于显示的数据源
var _dataSource:[String] = []
// 加载更多 状态 风火轮
var _aiv:UIActivityIndicatorView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// 数据源中的基础数据
for i in 0...2 {
_dataSource.append("\(i)")
}
// 初始下拉刷新控件
self.refreshControl = UIRefreshControl()
self.refreshControl?.attributedTitle = NSAttributedString(string: "Pull To Refresh")
self.refreshControl?.tintColor = UIColor.greenColor()
self.refreshControl?.addTarget(self, action: "refresh", forControlEvents: .ValueChanged)
// 加载更多按扭的背景视图
var tableFooterView:UIView = UIView()
tableFooterView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 44)
tableFooterView.backgroundColor = UIColor.greenColor()
self.tableView.tableFooterView = tableFooterView
// 加载更多的按扭
let loadMoreBtn = UIButton()
loadMoreBtn.frame = CGRectMake(0, 0, self.view.bounds.width, 44)
loadMoreBtn.setTitle("Load More", forState: .Normal)
loadMoreBtn.setTitleColor(UIColor.lightGrayColor(), forState: .Normal)
loadMoreBtn.addTarget(self, action: "loadMore:", forControlEvents: .TouchUpInside)
tableFooterView.addSubview(loadMoreBtn)
// 加载更多 状态 风火轮
_aiv = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
_aiv.center = loadMoreBtn.center
tableFooterView.addSubview(_aiv)
}
// 加载更多方法
func loadMore(sender:UIButton) {
sender.hidden = true
_aiv.startAnimating()
dispatch_async(dispatch_get_global_queue(0, 0), { () -> Void in
self._dataSource.append("\(self._dataSource[self._dataSource.count-1].toInt()! + 1)")
dispatch_async(dispatch_get_main_queue(), { () -> Void in
sleep(1)
self._aiv.stopAnimating()
sender.hidden = false
self.tableView.reloadData()
})
})
}
// 下拉刷新方法
func refresh() {
if self.refreshControl?.refreshing == true {
self.refreshControl?.attributedTitle = NSAttributedString(string: "Loading...")
}
dispatch_async(dispatch_get_global_queue(0, 0), { () -> Void in
self._dataSource.insert("\(self._dataSource[0].toInt()! - 1)", atIndex: 0)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
sleep(1)
self.refreshControl?.endRefreshing()
self.refreshControl?.attributedTitle = NSAttributedString(string: "Pull To Refresh")
self.tableView.reloadData()
})
})
}
// tableView dataSource
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return _dataSource.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let identifier = "cell"
var cell = tableView .dequeueReusableCellWithIdentifier(identifier) as? UITableViewCell
if cell == nil {
cell = UITableViewCell(style: .Default, reuseIdentifier: identifier)
}
cell?.textLabel?.text = "\(_dataSource[indexPath.row])"
return cell!
}
}
二:OC刷新界面代码:
#import "ViewController.h"
@interface ViewController ()
{
// 数据源
NSMutableArray * _dataSource;
// 风火轮
UIActivityIndicatorView * _aiv;
}
@end
@implementation ViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 初始数据源
_dataSource = [[NSMutableArray alloc] init];
// 基础数据
for (int i=0; i<3; i++) {
[_dataSource addObject:[NSString stringWithFormat:@"%d",i]];
}
// 刷新控件
self.refreshControl = [[UIRefreshControl alloc] init];
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull To Refresh"];
self.refreshControl.tintColor = [UIColor greenColor];
[self.refreshControl addTarget:self action:@selector(refresh) forControlEvents:UIControlEventValueChanged];
// 背景视图
UIView * tableFooterView = [[UIView alloc] init];
tableFooterView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 44);
tableFooterView.backgroundColor = [UIColor greenColor];
self.tableView.tableFooterView = tableFooterView;
// 加载更多按扭
UIButton * loadMoreBtn = [[UIButton alloc] init];
loadMoreBtn.frame = CGRectMake(0, 0, self.view.bounds.size.width, 44);
[loadMoreBtn setTitle:@"Load More" forState:UIControlStateNormal];
[loadMoreBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[loadMoreBtn addTarget:self action:@selector(loadMore:) forControlEvents:UIControlEventTouchUpInside];
[tableFooterView addSubview:loadMoreBtn];
// 风火轮
_aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_aiv.center = loadMoreBtn.center;
[tableFooterView addSubview:_aiv];
}
// 加载更多方法
- (void)loadMore:(UIButton *)sender
{
sender.hidden = YES;
[_aiv startAnimating];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[_dataSource addObject: [NSString stringWithFormat:@"%d", [_dataSource[_dataSource.count-1] intValue] + 1]];
dispatch_async(dispatch_get_main_queue(), ^{
sleep(1);
[_aiv stopAnimating];
sender.hidden = NO;
[self.tableView reloadData];
});
});
}
// 下拉刷新方法
- (void)refresh {
if (self.refreshControl.refreshing) {
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Loading..."];
}
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[_dataSource insertObject:[NSString stringWithFormat:@"%d", [_dataSource[0] intValue] -1] atIndex:0];
dispatch_async(dispatch_get_main_queue(), ^{
sleep(1);
[self.refreshControl endRefreshing];
self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Pull To Refresh"];
[self.tableView reloadData];
});
});
}
// tableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * identifier = @"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
cell.textLabel.text = _dataSource[indexPath.row];
return cell;
}
@end
使用OC和swift创建系统自带的刷新界面的更多相关文章
- Android调用系统自带的设置界面
Android有很多系统自带的设置界面,如设置声音,设置网络等. 在开发中可以调用这些系统自带的设置界面. 点击以下列表中的选项,就可以调出相应的系统自带的设置界面. 如点击“无线和网络设置”,可以调 ...
- OC与Swift创建pod
Cocoa pods 是iOS最常用的类库管理工具 OC的使用 删除源 sudo gem sources -r https://rubygems.org/ 添加源(使用淘宝的镜像,记住要用 ...
- 在OC代码中创建Swift编写的视图控制器
背景 近日在和一群朋友做项目,我和另一位同学负责iOS客户端,我是一直使用OC的,而他只会Swift,因此在我们分工协作之后,就需要把代码合在一起,这就牵扯到如何在TabbarController中添 ...
- [Swift通天遁地]二、表格表单-(4)使用系统自带的下拉刷新控件,制作表格的下拉刷新效果
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 如何使用win7自带的备份还原以及创建系统镜像------傻瓜式教程
对于经常鼓捣电脑的童鞋来说,装系统是一件极其平常的事情,不过系统装多了之后,我们会感到比较烦躁,因为每一次装系统意味着驱动的重新安装,程序的重新安装,每次这么鼓捣几次,半天时间就花在这上面了,效率是在 ...
- Win10使用自带功能创建系统映像备份时D盘被包含进去问题的解决
在使用Windows10系统时,使用Windows自带功能创建系统映像备份文件时碰到了一些问题,所以在此记录一下. 创建系统映像文件的步骤,如下: 1.打开 控制面板 -> 选择 系统和安全 - ...
- iOS开发——运行时OC篇&使用运行时获取系统的属性:使用自己的手势修改系统自带的手势
使用运行时获取系统的属性:使用自己的手势修改系统自带的手势 有的时候我需要实现一个功能,但是没有想到很好的方法或者想到了方法只是那个方法实现起来太麻烦,一或者确实为了装逼,我们就会想到iOS开发中最牛 ...
- iOS代码规范(OC和Swift)
下面说下iOS的代码规范问题,如果大家觉得还不错,可以直接用到项目中,有不同意见 可以在下面讨论下. 相信很多人工作中最烦的就是代码不规范,命名不规范,曾经见过一个VC里有3个按钮被命名为button ...
- iOS OC和Swift进行互相调用
有时候 ,我们会涉及到双向混合编程,特别是OC和swift的互相引用. swift调用oc的方法: 1.桥接文件,一般是swift工程,在创建一个oc文件时,系统自动添加(不用改名,直接默认即可) 2 ...
随机推荐
- ui router digest 10 time
refer : https://github.com/angular-ui/ui-router/issues/600 $urlRouterProvider.when("/", &q ...
- PCB的阻抗控制
多层板的结构: 通常我们所说的多层板是由芯板和半固化片互相层叠压合而成的,芯板是一种硬质的.有特定厚度的.两面包铜的板材,是构成印制板的基础材料.而半固化片构成所谓的浸润层,起到粘合芯板的作用,虽然也 ...
- MFC界面更新实现方法
1.更新窗口 即采用UpdateWindow()函数立即发送WM_PAINT消息更新整个窗口. void CEditTestDlg::OnBnClickedBtnSysUpdate() { CStri ...
- 【网贷投资手册】P2P行业揭秘
[网贷投资手册]P2P行业揭秘 (中国电子商务研究中心讯)如果你手头有100元,你会拿它来做什么?跟好朋友去吃一顿?跟女朋友去看场电影?还是……你会想到拿100元去投资吗?100元太少了,买一 ...
- js执行引擎(js解释器)
看字面理解,js执行引擎讲的就是将js代码转化为机器码并执行的过程. 一款 JavaScript 引擎是由 Brendan Eich 在网景的 Navigator 中开发的,它的名字叫做 Spider ...
- HDU2196-Computer
原题连接: http://acm.hdu.edu.cn/showproblem.php?pid=2196 思路: 好了,无敌了,经过昨晚4个钟头+今上午1个小时的奋战,这题终于被我AC了 收获的确是不 ...
- (转)在Android的webview中定制js的alert,confirm和prompt对话框的方法
1.首先继承android.webkit.WebChromeClient实现MyWebChromeClient. 2.在MyWebChromeClient.java中覆盖onJsAlert,onJsC ...
- [转]ANDROID仿IOS微信滑动删除_SWIPELISTVIEW左滑删除例子
转载:http://dwtedx.sinaapp.com/itshare_290.html 本例子实现了滑动删除ListView的Itemdemo的效果.大家都知道.这种创意是来源于IOS的.左滑删除 ...
- oracle group by rollup,decode,grouping,nvl,nvl2,nullif,grouping_id,group_id,grouping sets,RATIO_TO
干oracle 047文章12当问题,经验group by 声明.因此邂逅group by rollup,decode,grouping,nvl,nvl2,nullif,RATIO_TO_REPOR ...
- js生成随机数的方法小结
js生成随机数主要用到了内置的Math对象的random()方法.用法如:Math.random().它返回的是一个 0 ~ 1 之间的随机数.有了这么一个方法,那生成任意随机数就好理解了.比如实际中 ...