FJPullTableView 上拉加载更多的效果实现

本文介绍如何使用FJPullTableView类实现iOS应用中的上拉加载更多功能,包括导入类、使用方法、实现代理方法以及加载数据的流程。详细解释了如何在列表显示数据较多的情况下,通过滚动触发加载更多数据的功能。

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

FJPullTableView 上拉加载更多的效果实现

  (2013-06-05 11:21:27)
标签: 

uitableview

 

上拉刷新

 

fjpulltableview

 

it

分类: iOS开发

项目中由于列表显示的数据比较多,我们经常要用到上拉加载更多的功能,写了一个小类实现了此功能,具体使用方法如下:

 

1. 使用方法: 

(1)导入     #import "FJPullTableView.h"

(2)使用    绑定代理FJPullTableViewDelegate, UISCrollViewDelegate

定义变量  FJPullTableView *mytv;

    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];

实现代理方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    [mytv fjPullScrollViewDidScroll:scrollView];

}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    [mytv fjPullScrollViewDidEndDragging:scrollView];

}

- (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];

}

注意:  加载更多执行完后

1. 判断是否还要显示上拉刷新提示

[myth setIsNeedDisplay:YES];

2. 调用[mytv fjPullScrollViewDataSourceDidFinishedLoading:mytv];


使用方法比较简单,具体代码实现如下

 

#import

@class FJPullTableView;

@protocol FJPullTableViewDelegate

//程序中加载数据的代码在此方法中执行

- (void)beginToFetchData:(FJPullTableView *)tableView;


@end


@interface FJPullTableView : UITableView


@property (nonatomicassignid pullDelegate;

//该方法控制tableview是否显示上拉刷新提示,是否执行上拉刷新操作

- (void)setIsNeedDisplay:(BOOL)isNeedToDisplay;

- (void)fjPullScrollViewDidScroll:(UIScrollView *)scrollView;

- (void)fjPullScrollViewDidEndDragging:(UIScrollView *)scrollView;

//更多数据加载完成后需要调用此方法

- (void)fjPullScrollViewDataSourceDidFinishedLoading:(UIScrollView *)scrollView;

@end


#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(0self.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 - 5052020);

    }

    [indicatorView startAnimating];

    lbLoadingInfo.text = PULL_LOADING;

    [lbLoadingInfo addSubview:indicatorView];

}

- (void)hiddenIndicatorView {

    if (indicatorView) {

        [indicatorView stopAnimating];

        [indicatorView removeFromSuperview];

    }

    [lbLoadingInfo removeFromSuperview];

    hasLoadingInfo = NO;

}


@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值