上拉加载更多FJPullTableView文件的实现代码

本文介绍了一个名为FJPullTableView的自定义UITableView子类,它实现了iOS应用中常见的上拉加载更多功能。通过简单的集成步骤,开发者可以轻松地为UITableView添加上拉刷新的功能,同时还提供了加载过程中的状态提示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//

//  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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值