Permalink
Browse files

Integrate the pull to refresh into RCLTVC

  • Loading branch information...
1 parent 204a066 commit 89112b23c46957e868087c985920b38f46f70b4e @clawoo committed Nov 29, 2011
@@ -0,0 +1,32 @@
+//
+// RCLTableRefreshHeader.h
+// RCL
+//
+// Created by Clawoo on 10/3/11.
+// Copyright 2011 __MyCompanyName__. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+#define kRCLRefreshHeaderHeight 52.0f
+
+typedef enum {
+ RCLTableRefreshStateNormal,
+ RCLTableRefreshStateDragging,
+ RCLTableRefreshStateLoading
+} RCLTableRefreshState;
+
+@interface RCLTableRefreshHeader : UIView {
+ UILabel *refreshLabel_;
+ UILabel *lastRefreshLabel_;
+ UIImageView *refreshArrow_;
+ UIActivityIndicatorView *refreshSpinner_;
+
+ RCLTableRefreshState state_;
+}
+
+- (void)setLightStyle:(BOOL)lightStyle;
+- (void)setState:(RCLTableRefreshState)state;
+- (void)setLastRefreshDate:(NSDate *)lastRefreshDate;
+
+@end
@@ -0,0 +1,112 @@
+//
+// RCLTableRefreshHeader.m
+// RCL
+//
+// Created by Clawoo on 10/3/11.
+// Copyright 2011 __MyCompanyName__. All rights reserved.
+//
+
+#import "RCLTableRefreshHeader.h"
+
+@implementation RCLTableRefreshHeader
+
+- (id)init
+{
+ self = [self initWithFrame:CGRectMake(0, 0 - kRCLRefreshHeaderHeight, 320, kRCLRefreshHeaderHeight)];
+ if (self) {
+
+ }
+ return self;
+}
+
+- (id)initWithFrame:(CGRect)frame
+{
+ self = [super initWithFrame:frame];
+ if (self) {
+ self.backgroundColor = [UIColor clearColor];
+
+ refreshLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 320, 20)];
+ refreshLabel_.backgroundColor = [UIColor clearColor];
+ refreshLabel_.font = [UIFont boldSystemFontOfSize:12.0];
+ refreshLabel_.textAlignment = UITextAlignmentCenter;
+
+ lastRefreshLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(0, kRCLRefreshHeaderHeight - 23, 320, 20)];
+ lastRefreshLabel_.backgroundColor = [UIColor clearColor];
+ lastRefreshLabel_.font = [UIFont systemFontOfSize:12.0];
+ lastRefreshLabel_.textAlignment = UITextAlignmentCenter;
+
+ refreshArrow_ = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"RCLResources.bundle/arrow.png"]];
+ refreshArrow_.frame = CGRectMake((kRCLRefreshHeaderHeight - 27) / 2,
+ (kRCLRefreshHeaderHeight - 44) / 2,
+ 27, 44);
+
+ refreshSpinner_ = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
+ refreshSpinner_.frame = CGRectMake((kRCLRefreshHeaderHeight - 20) / 2, (kRCLRefreshHeaderHeight - 20) / 2, 20, 20);
+ refreshSpinner_.hidesWhenStopped = YES;
+
+ [self addSubview:refreshLabel_];
+ [self addSubview:lastRefreshLabel_];
+ [self addSubview:refreshArrow_];
+ [self addSubview:refreshSpinner_];
+ }
+ return self;
+}
+
+- (void)setLightStyle:(BOOL)lightStyle {
+ if (lightStyle) {
+ refreshLabel_.textColor = [UIColor lightTextColor];
+ lastRefreshLabel_.textColor = [UIColor lightTextColor];
+ refreshArrow_.image = [UIImage imageNamed:@"RCLResources.bundle/arrow-light.png"];
+ refreshSpinner_.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite;
+ }
+ else {
+
+ refreshLabel_.textColor = [UIColor darkTextColor];
+ lastRefreshLabel_.textColor = [UIColor darkTextColor];
+ refreshArrow_.image = [UIImage imageNamed:@"RCLResources.bundle/arrow.png"];
+ refreshSpinner_.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
+ }
+}
+
+- (void)setState:(RCLTableRefreshState)state {
+ state_ = state;
+ // Update the arrow direction and label
+ [UIView beginAnimations:nil context:NULL];
+ switch (state) {
+ case RCLTableRefreshStateNormal:
+ refreshLabel_.text = @"Pull down to refresh...";
+ refreshArrow_.hidden = NO;
+ refreshArrow_.transform = CGAffineTransformMakeRotation(M_PI * 2);
+ [refreshSpinner_ stopAnimating];
+ break;
+ case RCLTableRefreshStateDragging:
+ refreshLabel_.text = @"Release to refresh...";
+ refreshArrow_.transform = CGAffineTransformMakeRotation(M_PI);
+ break;
+ case RCLTableRefreshStateLoading:
+ [UIView setAnimationDuration:0.3];
+ refreshLabel_.text = @"Loading...";
+ refreshArrow_.hidden = YES;
+ [refreshSpinner_ startAnimating];
+ break;
+
+ default:
+ break;
+ }
+ [UIView commitAnimations];
+}
+
+- (void)setLastRefreshDate:(NSDate *)lastRefreshDate {
+ NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
+ [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
+ [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
+
+ lastRefreshLabel_.text = [NSString stringWithFormat:@"Last updated: %@",[dateFormatter stringFromDate:lastRefreshDate]];
+}
+
+- (void)dealloc
+{
+ [super dealloc];
+}
+
+@end
@@ -7,6 +7,7 @@
//
#import <UIKit/UIKit.h>
+#import "RCLTableRefreshHeader.h"
#define kRCLTableViewResultsPerPage 10
@@ -15,36 +16,61 @@
with pagination and other functionality
*/
@interface RCLTableViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
- NSMutableArray *dataSource_;
- NSInteger totalResults_;
- NSInteger resultsPerPage_;
- BOOL isLoadingNextPage_;
- BOOL morePagesAreAvailable_;
- NSDate *lastRefreshDate_;
- BOOL reloadDataWhenStoppedDecelerating_;
- UITableView *tableView_;
- /*!
- The 'loading more results' loading view
- */
- UIView *loadingView_;
+
+ BOOL isPaginationEnabled_;
+ BOOL isPullToRefreshEnabled_;
+
+ UITableView *tableView_;
+ NSMutableArray *dataSource_;
+ UIView *loadingView_;
+
+ NSInteger totalResults_;
+ NSInteger resultsPerPage_;
+ BOOL isLoadingNextPage_;
+ BOOL morePagesAreAvailable_;
+ BOOL reloadDataWhenStoppedDecelerating_;
+
+ // pull to refresh
+ RCLTableRefreshHeader *refreshHeaderView_;
+ NSDate *lastRefreshDate_;
+ BOOL isDragging_;
+ BOOL isReloading_;
+
}
@property (nonatomic, retain) NSMutableArray *dataSource;
-@property (nonatomic, retain) NSDate *lastRefreshDate;
@property (nonatomic, retain) IBOutlet UITableView *tableView;
-
+@property (nonatomic, retain) NSDate *lastRefreshDate;
+@property (nonatomic, assign) BOOL isPaginationEnabled;
+@property (nonatomic, assign) BOOL isPullToRefreshEnabled;
/*!
- This method is called when a new page of data is needed.
+ This method is called when a new page of data is needed. Your implementation
+ should override it and call super.
@param resultsCount The number of results requested (usually kRCLTableViewResultsPerPage)
@param fromIndex The index from where to load results from.
*/
- (void)startLoadingResults:(NSInteger)resultsCount fromIndex:(NSInteger)fromIndex;
/*!
- This method should be manually called when results have been loaded.
+ This method should be manually called when results have been loaded. Your implementation
+ should call it after the results have been loaded.
@param resultsCount The number of results that have actually been added to the dataSource.
@param fromIndex The index the results start from.
*/
- (void)didEndLoadingResults:(NSInteger)resultsCount fromIndex:(NSInteger)fromIndex;
+
+/*!
+ This method is called when the user has completed a PTR gesture.
+ The table view controller should override this method and start loading data
+ in background
+ */
+- (void)startReloadingResults;
+
+/*!
+ This method must be called manually after the reload of the data has been
+ completed.
+ */
+- (void)didEndReloadingResults;
+
@end
Oops, something went wrong.

0 comments on commit 89112b2

Please sign in to comment.