在项目开发过程中为了更好的体验经常会用到下拉刷新更新数据。当前比较火的EGOTableViewPullRefresh只实现了下拉功能,而没有上拉的功能。这里介绍一个同时集成下拉刷新和上拉加载更多的类库EGOTableViewPullRefresh
下载地址:https://github.com/MPK-Github/tableViewPullRefresh
用法很简单,如下:
- 添加 QuartzCore.framework 到你的工程中。
- 将 EGOTableViewPullRefresh 拖到你的工程目录下。
- 查看 PullTableView.h 文件可用的属性。
- 添加一个PullTableView 到你代码中,实现PullTableViewDelegate委托方法
//
// ViewController.m
// tableViewPullRefresh
//
// Created by imac on 15/9/13.
// Copyright (c) 2015年 mapengkun. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// 1.创建表视图
_pullTableView = [[PullTableView alloc] initWithFrame:CGRectMake(0, 64, 320, 568- 64) style:UITableViewStyleGrouped];
// 2.设置相关属性,可以点击PullTableView文件,查看相关属性
_pullTableView.pullArrowImage = [UIImage imageNamed:@"blackArrow"];
_pullTableView.pullBackgroundColor = [UIColor yellowColor];
_pullTableView.pullTextColor = [UIColor blackColor];
// 3.实现数据源协议和代理,(记得在.h文件里实现)
_pullTableView.dataSource = self;
_pullTableView.delegate = self;
// 4.添加到主视图中
[self.view addSubview:_pullTableView];
}
#pragma mark -Refresh and LoadMoredata Method
// 刷新表视图的方法
- (void)refreshTable
{
_pullTableView.pullLastRefreshDate = [NSDate date];
_pullTableView.pullTableIsRefreshing = NO;
}
// 加载更多数据的方法
- (void)loadMoreDataToTable
{
_pullTableView.pullTableIsLoadingMore = NO;
}
#pragma mark - UTTableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 8;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentify = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentify];
}
cell.textLabel.text = [NSString stringWithFormat:@"Row %ld",indexPath.row];
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSString stringWithFormat:@"Section %ld begins here!",section];
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return [NSString stringWithFormat:@"Section %ld ends here!",section];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 20;
}
#pragma mark - PullTableDelegate
- (void)pullTableViewDidTriggerRefresh:(PullTableView *)pullTableView
{
[self performSelector:@selector(refreshTable) withObject:nil afterDelay:3.0];
}
- (void)pullTableViewDidTriggerLoadMore:(PullTableView *)pullTableView
{
[self performSelector:@selector(loadMoreDataToTable) withObject:nil afterDelay:3.0];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
希望对你们有所帮助,欢迎到博客进行转载