使用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

一: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