Skip to content
Browse files

added basic support for pagination

  • Loading branch information...
1 parent a13675f commit fa003b8c7343f22ccbeb1d1991d96d067dde0ce0 @ettore committed Oct 30, 2012
Showing with 228 additions and 14 deletions.
  1. +28 −0 CLCGMoreCell.h
  2. +85 −0 CLCGMoreCell.m
  3. +31 −3 CLCGTVVC.h
  4. +84 −11 CLCGTVVC.m
View
28 CLCGMoreCell.h
@@ -0,0 +1,28 @@
+//
+// CLCGMoreCell.h
+// Goodreads
+//
+// Created by Ettore Pasquini on 10/29/12.
+//
+//
+
+
+@interface CLCGMoreCell : UITableViewCell
+{
+ UIActivityIndicatorView *mSpinner;
+}
+
+// designated initializer
+-(id)initReusingId:(NSString*)reuse_id withText:(NSString*)text;
+
+// uses "More..." as default text
+-(id)initReusingId:(NSString*)reuse_id;
+
+// the More cell height is constant
++(CGFloat)cellHeight;
+
+-(void)didStartRequestingMore;
+-(void)didStopRequestingMore;
+
+@end
+
View
85 CLCGMoreCell.m
@@ -0,0 +1,85 @@
+//
+// CLCGMoreCell.m
+// Goodreads
+//
+// Created by Ettore Pasquini on 10/29/12.
+//
+//
+
+#import "clcg_macros.h"
+#import "UIViewCategory.h"
+#import "CLCGMoreCell.h"
+
+@implementation CLCGMoreCell
+
+
+-(void)dealloc
+{
+ CLCG_REL(mSpinner);
+ [super dealloc];
+}
+
+
+-(id)initReusingId:(NSString*)reuse_id
+{
+ return [self initReusingId:reuse_id withText:CLCG_LOC(@"More...")];
+}
+
+
+// designated initializer
+-(id)initReusingId:(NSString*)reuse_id withText:(NSString*)text
+{
+ self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse_id];
+ if (self) {
+ [self setSelectionStyle:UITableViewCellSelectionStyleBlue];
+ [[self textLabel] setNumberOfLines:1];//set to 0 and calc height dynamically
+ [[self textLabel] setTextColor:[UIColor blackColor]];
+ [[self textLabel] setBackgroundColor:[UIColor clearColor]];
+ [[self textLabel] setTextAlignment:UITextAlignmentCenter];
+ [[self textLabel] setText:text];
+ [[self textLabel] setFont:[UIFont boldSystemFontOfSize:18]];
+
+ mSpinner = [[UIActivityIndicatorView alloc]
+ initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
+ [self setAccessoryView:mSpinner];
+ [mSpinner setHidden:YES];
+ }
+
+ return self;
+}
+
+
+-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuse_id
+{
+ return [self initReusingId:reuse_id];
+}
+
+
+-(void)didStartRequestingMore
+{
+ [mSpinner startAnimating];
+ [mSpinner setHidden:NO];
+}
+
+
+-(void)didStopRequestingMore
+{
+ [mSpinner stopAnimating];
+ [mSpinner setHidden:YES];
+}
+
+
+-(void)layoutSubviews
+{
+ [super layoutSubviews];
+ [[self textLabel] setW:[self width]];
+}
+
+
++(CGFloat)cellHeight
+{
+ return 50.0f; //TODO-XX
+}
+
+
+@end
View
34 CLCGTVVC.h
@@ -9,6 +9,14 @@
#import "CLCGVC.h"
+// used to deque cells in table-view
+#define CLCGTVVC_CID @"CLCGTVVC_CID"
+#define CLCGTVVC_MORE_CID @"CLCGTVVC_MORE_CID"
+
+// default number of items to be loaded in table-view
+#define PER_PAGE_DEFAULT 20
+
+
enum CLCGLoadingState {
CLCG_NOT_LOADED,
CLCG_LOADING,
@@ -23,13 +31,29 @@ enum CLCGLoadingState {
*/
@interface CLCGTVVC : CLCGVC<UITableViewDelegate,UITableViewDataSource>
{
- UITableViewStyle mStyle;
- UITableView *mTableView;
- enum CLCGLoadingState mLoadState;
+ UITableViewStyle mStyle;
+ UITableView *mTableView;
+ enum CLCGLoadingState mLoadState;
+
+ // array containing the models of the table-view items.
+ NSMutableArray *mItems;
+
+ // support for pagination. Disabled by default.
+ int mPage;
+ int mPerPage;
+ int mItemsTotal; //total number of items
+ int mItemsEnd; //current number of items being displayed
+ NSString *mMoreButtonText;
}
@property(nonatomic,retain) IBOutlet UITableView *tableView;
@property(nonatomic,assign) enum CLCGLoadingState loadState;
+@property(nonatomic,assign) int page;
+@property(nonatomic,assign) int perPage;
+@property(nonatomic,assign) BOOL supportsPagination;
+@property(nonatomic,assign) int itemsTotal;
+@property(nonatomic,assign) int itemsEnd;
+@property(nonatomic,retain) NSString *moreButtonText;
// still the designated initializer. Defaults style to UITableViewStylePlain.
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
@@ -40,4 +64,8 @@ enum CLCGLoadingState {
// deselects all currently selected rows.
-(void)deselectAll:(BOOL)animated;
+-(UITableViewCell*)tableView:(UITableView*)tv moreButtonCellForRow:(NSIndexPath*)ip;
+
+-(BOOL)isMoreRow:(NSIndexPath*)ip;
+
@end
View
95 CLCGTVVC.m
@@ -6,24 +6,49 @@
// Copyright (c) 2012 Cubelogic. All rights reserved.
//
+
#import "CLCGTVVC.h"
-#import "clcg_macros.h"
+#import "CLCGMoreCell.h"
+
@implementation CLCGTVVC
+
@synthesize loadState = mLoadState;
+@synthesize page = mPage;
+@synthesize perPage = mPerPage;
+@synthesize itemsTotal = mItemsTotal;
+@synthesize itemsEnd = mItemsEnd;
+@synthesize moreButtonText = mMoreButtonText;
+
//-----------------------------------------------------------------------------
#pragma mark - Init, dealloc, memory mgmt
+-(void)dealloc
+{
+ CLCG_REL(mItems);
+ CLCG_REL(mMoreButtonText);
+ [super dealloc];
+}
+
+
+// this is called by the super class dealloc and viewDidUnload
+-(void)releaseRetainedSubviews
+{
+ CLCG_REL(mTableView);
+ [super releaseRetainedSubviews];
+}
+
+
// designated initializer
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
mStyle = UITableViewStylePlain;
- mLoadState = CLCG_NOT_LOADED;
+ [self doInitCore];
}
return self;
}
@@ -34,17 +59,17 @@ -(id)initWithStyle:(UITableViewStyle)style
self = [super initWithNibName:nil bundle:nil];
if (self) {
mStyle = style;
- mLoadState = CLCG_NOT_LOADED;
+ [self doInitCore];
}
return self;
}
-// this is called by the super class dealloc and viewDidUnload
--(void)releaseRetainedSubviews
+-(void)doInitCore
{
- CLCG_REL(mTableView);
- [super releaseRetainedSubviews];
+ mLoadState = CLCG_NOT_LOADED;
+ mPage = 1;
+ mPerPage = -1; // pagination is disabled by default
}
@@ -121,22 +146,70 @@ -(void)setEditing:(BOOL)editing animated:(BOOL)animated
}
+-(BOOL)isMoreRow:(NSIndexPath*)ip
+{
+ return (mItemsEnd < mItemsTotal && [ip row] == [mItems count]);
+}
+
+
+-(UITableViewCell*)tableView:(UITableView*)tv moreButtonCellForRow:(NSIndexPath*)ip
+{
+ CLCGMoreCell *cell;
+
+ cell = (CLCGMoreCell*)[tv dequeueReusableCellWithIdentifier:CLCGTVVC_MORE_CID];
+
+ if (cell == nil) {
+ cell = [[CLCGMoreCell alloc] initReusingId:CLCGTVVC_MORE_CID withText:mMoreButtonText];
+ [cell autorelease];
+ }
+
+ if (mLoadState != CLCG_LOADING)
+ [cell didStopRequestingMore];
+
+ return cell;
+}
+
//-----------------------------------------------------------------------------
#pragma mark - UITableViewDataSource protocol
--(NSInteger)tableView:(UITableView*)tv numberOfRowsInSection:(NSInteger)section
+-(NSInteger)numberOfSectionsInTableView:(UITableView*)tv
+{
+ return 1;
+}
+
+
+-(NSInteger)tableView:(UITableView*)tv numberOfRowsInSection:(NSInteger)sect
{
- return 0;
+ if (mItemsEnd < mItemsTotal && [self supportsPagination])
+ return [mItems count] + 1; //for the "More..." button
+ else
+ return [mItems count];
}
--(UITableViewCell*)tableView:(UITableView*)tv
- cellForRowAtIndexPath:(NSIndexPath*)indexPath
+-(UITableViewCell*)tableView:(UITableView*)tv cellForRowAtIndexPath:(NSIndexPath*)ip
{
return nil;
}
+//------------------------------------------------------------------------------
+#pragma mark - Pagination Support
+
+
+-(BOOL)supportsPagination
+{
+ return mPerPage > 0;
+}
+
+
+-(void)setSupportsPagination:(BOOL)flag
+{
+ mPerPage = (flag ? PER_PAGE_DEFAULT : -1);
+}
+
+
@end
+

0 comments on commit fa003b8

Please sign in to comment.
Something went wrong with that request. Please try again.