使用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 ...
随机推荐
- 如何统一修改 Altium Designer 中的字符大小
如下图 1 所示: Q1. Q2. C1. C2. R1 等等的字符你想统一修改他们的大小.原来是 Text Height( 100mil), Text Width( 12mil),想改成 Text ...
- Python模块如何安装 并确认模块已经安装好?
看自己有没有安装好,最简单的办法在可以再控制台下: C:\Users\sony>python Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC ...
- Android 解决listview中checkBox错位选择
假如ListView,分成2页(或者设置数据可以纵向拉,可隐藏),每页3条数据,每个Listview的Item 里面有个checkBox,现在,当我选择第一页的前两天数据,翻到第二页,竟然第二页后两条 ...
- 普通的101键盘在Mac上的键位对应
为了方便,搞了一个普通的101有线全键盘 + Magic TrackPad配Macbook. 然后发现了一个小问题,按键对应似乎不像我想的那么完美,F1~F12和Macbook不对应,于 ...
- COJ 0034 动态的数字三角形
题解:简单dp吧. 自顶向下的写法: #include<iostream> #include<cstdio> #include<cmath> #include< ...
- POJ-2240
Arbitrage Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19063 Accepted: 8069 Descri ...
- HP DL160 Gen9服务器集群部署文档
HP DL160 Gen9服务器集群部署文档 硬件配置=======================================================Server Memo ...
- iBatis查询结果部分为null的解决办法
今天第一天接触iBatis,没有系统学习过,遇到了一个简单却闹心的错误:用iBatis查询数据库中某个表的多列结果作为一个对象返回时,会出现对象的部分属性为null值得错误.例如,查询用户表中的用户I ...
- Freemarker生成静态代码实例
1.static.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http ...
- UVALive 6525 Attacking rooks 二分匹配 经典题
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4536">点击打开链接 题意: ...