Coder Social home page Coder Social logo

sfpullrefresh's Introduction

SFPullRefresh (support ios5+)

SFPullRefresh provides a very simple way to implement pull refresh and load more, supports customizing refresh or loadmore control with your own view.

Installation with CocoaPods

pod 'SFPullRefresh'

Usage

Basic

Add pull refresh handler and load more handler to your tableView or collectionView, and when you get data, you should call [self.table sf_finishLoading] to end loading animation, if there is no more data, you should call [self.table sf_reachEndWithText:@"reach end message"].

- (void)viewDidLoad
{
  ...
  __weak YourViewController *wkself = self; //you must use wkself to break the retain cycle
  [self.table sf_addRefreshHandler:^{
      wkself.page = 0;
      [wkself loadStrings];
  }];
    
  [self.table sf_addLoadMoreHandler:^{
      [wkself loadStrings];
  }];
  
  ...
}

- (void)loadStrings
{
    [self requestDataAtPage:self.page success:^(NSArray *strings) {
        if (self.table.sf_isRefreshing) {
            [self.items removeAllObjects];
        }
        [self.items addObjectsFromArray:strings];
        self.page++;
        if (strings.count<10) {
            [self.table sf_reachEndWithText:@"加载完毕"];
        }
        [self.table sf_finishLoading];

    } failure:^(NSString *msg) {
        [self.items removeAllObjects];
        [self.table sf_finishLoading];
    }];
}

Customization

You can customize refresh or load more control with your own view.

    CustomRefreshControl *customRefreshControl = [[CustomRefreshControl alloc] initWithFrame:CGRectMake(0, 0,  [UIScreen mainScreen].bounds.size.width, 60)];
    
    __weak YourViewController *wkself = self; //you must use wkself to break the retain cycle
    [self.table sf_addRefreshHandler:^{
        wkself.page = 0;
        [wkself loadStrings];
    } customRefreshControl:customRefreshControl];

The custom view must conforms to protocol SFRefreshControlDelegate or SFLoadMoreControlDelegate.

@protocol SFRefreshControlDelegate <NSObject>
- (void)willRefreshWithProgress:(CGFloat)progress;
- (void)beginRefreshing;
- (void)endRefreshing;

@optional
- (void)setTintColor:(UIColor *)tintColor;
@end

@protocol SFLoadMoreControlDelegate <NSObject>
- (void)beginLoading;
- (void)endLoading;
- (void)reachEndWithText:(NSString *)text;

@optional
- (void)setTintColor:(UIColor *)tintColor;
@end

Enjoy it!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.