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!

sfpullrefresh's People

Contributors

nealsun avatar shaohuachen avatar sofach avatar youxiaomingbai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

sfpullrefresh's Issues

进入页面不会自动刷新

[[UINavigationBar appearance] setTranslucent:NO];
更改NavigationBar的属性之后

[self.tableView sf_addRefreshHandler:^{
}];

这个方法不会调用了

creash

如果在继承UITableVIewController 的试图上加下啦刷新 从这个界面进入下一界面再回来 再进入到下一界面 会creash

2015-10-07 17:46:32.634 SFPullRefresh[17245:263139] *** Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer <SFPullRefreshContext 0x7fc65372ad90> for the key path "contentSize" from <UITableView 0x7fc653812600> because it is not registered as an observer.'

sf_addLoadMoreHandler: crash

I get following crash message:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UILabel sf_willMoveToSuperview:]: unrecognized selector sent to instance 0x7f93785cedb0'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010bc28c65 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010b8c1bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010bc300ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010bb8613c ___forwarding___ + 988
    4   CoreFoundation                      0x000000010bb85cd8 _CF_forwarding_prep_0 + 120
    5   yjj                                 0x0000000108b311ce -[UIScrollView(SFPullRefresh) sf_willMoveToSuperview:] + 62
    6   UIKit                               0x000000010a672a6f -[UIView(Internal) _addSubview:positioned:relativeTo:] + 483
    7   yjj                                 0x0000000108b2d54e -[SFLoadMoreControl setup] + 686
    8   yjj                                 0x0000000108b2d270 -[SFLoadMoreControl initWithFrame:] + 304
    9   yjj                                 0x0000000108b30e44 -[UIScrollView(SFPullRefresh) sf_addLoadMoreHandler:customLoadMoreControl:position:] + 612
    10  yjj                                 0x0000000108b30ba9 -[UIScrollView(SFPullRefresh) sf_addLoadMoreHandler:customLoadMoreControl:] + 105
    11  yjj                                 0x0000000108b30b20 -[UIScrollView(SFPullRefresh) sf_addLoadMoreHandler:] + 64

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.