GithubHelp home page GithubHelp logo

mausvargas / nmpaginator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nmondollot/nmpaginator

0.0 2.0 0.0 287 KB

NMPaginator is a simple Objective-C class that handles pagination for you.

nmpaginator's Introduction

NMPaginator

NMPaginator is a simple Objective-C class that handles pagination for you. It makes it easy to display results from API webservices that take page and per_page parameters.

e.g. Flickr API :

http://api.flickr.com/services/rest/?method=flickr.photos.search&text=beach&per_page=20&page=2

The demo project also includes a UITableView that automatically loads the next page of results as you scroll down.

Example

NMPaginator with Flickr API

How to install

With Cocoapods

Add this to your Podfile:

pod 'NMPaginator', '~> 1.0.0'

For more information on how to set up Cocoapods, check out the official site.

Manually

Copy NMPaginator.h and NMPaginator.m to your project. That's it.

How to use

Custom Paginator Class

You need to sublass NMPaginator and implement the fetchResultsWithPage:pageSize: method :

// MyPaginator.h
#import <Foundation/Foundation.h>
#import "NMPaginator.h"
@interface MyPaginator : NMPaginator
@end

// MyPaginator.m
#import "MyPaginator.h"
@implementation MyPaginator
- (void)fetchResultsWithPage:(NSInteger)page pageSize:(NSInteger)pageSize
{
    // you code goes here
    // once you receive the results for the current page, just call [self receivedResults:results total:total];
}
@end

ViewController

In your ViewController header file, conform to NMPaginatorDelegate protocol and add a property for the paginator :

// ViewController.h
@interface ViewController () <NMPaginatorDelegate>
@property (nonatomic, strong) MyPaginator *paginator;
@end

In your ViewController implementation file, instantiate the paginator in viewDidLoad (for instance) :

// ViewController.m
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.myPaginator = [[MyPaginator alloc] initWithPageSize:10 delegate:self];
}

And ask for results like this :

// ViewController.m
[self.myPaginator fetchNextPage];

You will get the results through the delegate method :

// ViewController.m
- (void)paginator:(id)paginator didReceiveResults:(NSArray *)results 
{
    // handle new results
}

To make the UITableView work, please have a look at the demo project.

Credits

My name is Nicolas Mondollot, you can follow me on twitter.

To demonstrate NMPaginator, I used the FlickFetcher class from the great Stanford CS193P course.

License

Do whatever you want with this piece of code (commercially or free). Attribution would be nice though.

nmpaginator's People

Contributors

nmondollot avatar hydex86 avatar

Watchers

Mauricio Vargas avatar James Cloos avatar

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.