Last active
December 13, 2015 19:38
-
-
Save NickTitle/4963869 to your computer and use it in GitHub Desktop.
Super lightweight pull-to-refresh functionality for iOS, in 10 new lines of code
(This could be way more elegant with more lines of code, but the core is here in just ten.)
**LAST UPDATE** Increased lines cause I'm not proving a point anymore :P
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //--------------------------------------------------------// | |
| //MyTableViewHolder.h ==> add a property for a UITableView and a UILabel | |
| @interface MyTableViewHolder : UIViewController <UITableViewDataSource, UITableViewDelegate> | |
| @property (nonatomic, retain) UITableView *myTableView; | |
| @property (nonatomic, retain) UILabel *scrollLabel; | |
| //--------------------------------------------------------// | |
| //MyTableViewholder.m ==> insantiate and add the elements and a view to hold the UILabel, then grab events for scrolling and releasing the UIScrollView of the UITableView | |
| @synthesize myTableView; | |
| @synthesize scrollLabel; | |
| int refreshHeight = 88; | |
| - (void) viewDidLoad { | |
| // .. more initializing stuff here | |
| if (myTableView == nil) { | |
| myTableView = [UITableView new]; | |
| [myTableView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) ]; | |
| } | |
| [myTableView setDelegate :self]; | |
| [myTableView setDataSource :self]; | |
| UIView *refreshView = [UIView new]; | |
| [refreshView setFrame:CGRectMake(0, -(refreshHeight), myTableView.frame.size.width, (refreshHeight) ) ]; | |
| scrollLabel = [UILabel new]; | |
| [scrollLabel setFrame:CGRectMake(0, 0, newsTableView.frame.size.width, 76) ]; | |
| [scrollLabel setText:@"Pull to Refresh"]; | |
| [self.view addSubview :myTableView]; | |
| [refreshView addSubview :scrollLabel]; | |
| [myTableView addSubview :refreshView]; | |
| // ..more initializing stuff here | |
| } | |
| - (void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { | |
| if (scrollView.contentOffset.y < -(refreshHeight) ) { | |
| [self methodToRefreshDataAndReloadTable]; | |
| } | |
| } | |
| - (void) scrollViewDidScroll:(UIScrollView *)scrollView { | |
| if (scrollView.contentOffset.y < -(refreshHeight) ) { | |
| [self.scrollLabel setText:@"RELEASE TO REFRESH"]; | |
| } | |
| else { | |
| [self.scrollLabel setText:@"PULL TO REFRESH"]; | |
| } | |
| } | |
| //--------------------------------------------------------// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment