//
// FJPullTableView.h
// 上拉加载更多
//
// Created by U箱超市 on 14-4-4.
// Copyright (c) 2014年 姚琪. All rights reserved.
//
#import <UIKit/UIKit.h>
@class FJPullTableView;
@protocol FJPullTableViewDelegate
//程序中加载数据的代码在此方法中执行
- (void)beginToFetchData:(FJPullTableView *)tableView;
@end
@interface FJPullTableView : UITableView
@property (nonatomic, assign) id pullDelegate;
//该方法控制tableview是否显示上拉刷新提示,是否执行上拉刷新操作
- (void)setIsNeedDisplay:(BOOL)isNeedToDisplay;
- (void)fjPullScrollViewDidScroll:(UIScrollView *)scrollView;
- (void)fjPullScrollViewDidEndDragging:(UIScrollView *)scrollView;
//更多数据加载完成后需要调用此方法
- (void)fjPullScrollViewDataSourceDidFinishedLoading:(UIScrollView *)scrollView;
@end
//
// FJPullTableView.m
// 上拉加载更多
//
// Created by U箱超市 on 14-4-4.
// Copyright (c) 2014年 姚琪. All rights reserved.
//
#import "FJPullTableView.h"
#define PULL_LOAD_MORE @"上拉加载更多"
#define PULL_TO_LOAD @"松开即可加载"
#define PULL_LOADING @"加载中..."
@interface FJPullTableView () {
UILabel *lbLoadingInfo;
BOOL hasLoadingInfo;
BOOL isLoading;
BOOL isNeedDisplay;
UIActivityIndicatorView *indicatorView;
}
@end
@implementation FJPullTableView
@synthesize pullDelegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
isNeedDisplay = YES;
}
return self;
}
//设置tableview是否显示上拉刷新提示
- (void)setIsNeedDisplay:(BOOL)isNeedToDisplay {
isNeedDisplay = isNeedToDisplay;
}
//当tableview向上滚动时调用此方法
- (void)fjPullScrollViewDidScroll:(UIScrollView *)scrollView {
if (isNeedDisplay && !isLoading) {
if (scrollView.contentOffset.y + scrollView.frame.size.height > scrollView.contentSize.height + 60) {
lbLoadingInfo.text = PULL_TO_LOAD;
} else {
if (!lbLoadingInfo) {
lbLoadingInfo = [[UILabel alloc] initWithFrame:CGRectZero];
lbLoadingInfo.backgroundColor = [UIColor clearColor];
if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 6.0) {
lbLoadingInfo.textAlignment = NSTextAlignmentCenter;
} else {
lbLoadingInfo.textAlignment = UITextAlignmentCenter;
}
lbLoadingInfo.font = [UIFont systemFontOfSize:12];
lbLoadingInfo.textColor = [UIColor grayColor];
}
if (!hasLoadingInfo) {
lbLoadingInfo.frame = CGRectMake(0, self.contentSize.height,self.frame.size.width, 30);
[self addSubview:lbLoadingInfo];
hasLoadingInfo = YES;
}
lbLoadingInfo.text = PULL_LOAD_MORE;
}
}
}
//当tableview结束拖拽时调用此方法
- (void)fjPullScrollViewDidEndDragging:(UIScrollView *)scrollView {
if (isNeedDisplay && !isLoading) {
if (scrollView.contentOffset.y + scrollView.frame.size.height > scrollView.contentSize.height + 60) {
isLoading = YES;
//执行加载更多操作
if ([pullDelegate respondsToSelector:@selector(beginToFetchData:)]) {
[self showIndicatorView];
//执行代理方法,进行数据加载
[pullDelegate beginToFetchData:self];
}
}
}
}
//加载数据结束时执行此操作
- (void)fjPullScrollViewDataSourceDidFinishedLoading:(UIScrollView *)scrollView {
isLoading = NO;
[self hiddenIndicatorView];
[self reloadData];
}
- (void)showIndicatorView {
if (!indicatorView) {
indicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicatorView.frame = CGRectMake(self.frame.size.width/2 - 50, 5, 20, 20);
}
[indicatorView startAnimating];
lbLoadingInfo.text = PULL_LOADING;
[lbLoadingInfo addSubview:indicatorView];
}
- (void)hiddenIndicatorView {
if (indicatorView) {
[indicatorView stopAnimating];
[indicatorView removeFromSuperview];
}
[lbLoadingInfo removeFromSuperview];
hasLoadingInfo = NO;
}
@end
//
// ViewController.h
// 上拉加载更多
//
// Created by U箱超市 on 14-4-4.
// Copyright (c) 2014年 姚琪. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "FJPullTableView.h"
@interface ViewController : UITableViewController<FJPullTableViewDelegate, UIScrollViewDelegate>
{
FJPullTableView *mytv;
NSInteger totalCount;
}
@end
//
// ViewController.m
// 上拉加载更多
//
// Created by U箱超市 on 14-4-4.
// Copyright (c) 2014年 姚琪. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
// self.view.backgroundColor = [UIColor brownColor];
//导航栏中字体颜色的设置
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0 , 100, 44)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.font = [UIFont systemFontOfSize:15];
titleLabel.textColor = [UIColor whiteColor];//设置文本颜色
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.text = @"Settings";
self.navigationItem.titleView = titleLabel;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
//导航栏背景图片的设置
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"header.gif"] forBarMetrics:UIBarMetricsDefault];
mytv = [[FJPullTableView alloc] initWithFrame:CGRectMake(10, 50, 200, 200)];
mytv.delegate = self;
mytv.dataSource = self;
mytv.pullDelegate = self;
[mytv setIsNeedDisplay:YES];
[self.view addSubview:mytv];
}
#pragma mark --实现代理的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
[mytv fjPullScrollViewDidScroll:scrollView];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
[mytv fjPullScrollViewDidEndDragging:scrollView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)beginToFetchData:(FJPullTableView *)tableView
{
NSLog(@"start load");
//开始执行加载方法
//模拟执行加载方法
[self performSelector:@selector(doLoad) withObject:nil afterDelay:3];
}
- (void)doLoad {
NSLog(@"load done");
totalCount += 10;
if (totalCount > 30) {
[mytv setIsNeedDisplay:NO];
}
[mytv fjPullScrollViewDataSourceDidFinishedLoading:mytv];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
// Return the number of rows in the section.
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = @"上拉加载更多";
return cell;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
/*
#pragma mark - Navigation
// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end