Skip to content

Instantly share code, notes, and snippets.

@exorcyze
Last active December 11, 2015 20:48
Show Gist options
  • Save exorcyze/4657281 to your computer and use it in GitHub Desktop.
Save exorcyze/4657281 to your computer and use it in GitHub Desktop.
SampleViewController using EXTableView, EXPullToRefresh and base DataService calls to implement a functional twitter view.
#import "SampleViewController.h"
#import "EXTableView.h"
#import "SampleDataService.h"
#import "EXPullToRefresh.h"
@interface SampleViewController ()
- (void) setupView;
- (void) refreshData;
@end
@implementation SampleViewController {
NSArray *mydata;
EXTableView *mytable;
}
#pragma mark - View Lifecycle
- (id) init {
self = [super init];
if ( self ) {
self.view = [[UIView alloc] initWithFrame:CGRectMake( 0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height)];
[self setupView];
[self refreshData];
}
return self;
}
- (void) viewWillLayoutSubviews {
[super viewWilLayoutSubviews];
[mytable alignSizeWithRightPadding:0 andBottomPadding:0];
}
#pragma mark - Private Methods
- (void) setupView {
mytable = [[EXTableView alloc] initWithFrame:CGRectMake( 0, 0, SCREEN_SIZE.width, SCREEN_SIZE.height ) style:UITableViewStylePlain];
[mytable setBlockCellForRowCreate:^UITableViewCell *(NSIndexPath *indexPath) {
return [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:EX_TABLE_VIEW_CELL_IDENTIFIER];
}];
[mytable setBlockCellForIndexPath:^(UITableView *tableView, UITableViewCell *tableViewCell, NSObject *rowData, NSIndexPath *indexPath) {
NSDictionary *item = (NSDictionary *)rowData;
tableViewCell.textLabel.text = [item objectForKey:@"from_user"];
tableViewCell.detailTextLabel.text = [item objectForKey:@"text"];
}];
[mytable setBlockCellForRowSelect:^(UITableView *tableView, NSObject *rowData, NSIndexPath *indexPath) {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSLog( @"Selected %i", indexPath.row );
}];
[self.view addSubview:mytable];
__block typeof (self) weakself = self;
[mytable addPullToRefreshWithBlock:^(EXPullToRefresh *refreshControl) { [weakself refreshData]; }];
}
- (void) refreshData {
SampleDataService *myservice = [[SampleDataService alloc] init];
[myservice getTwitterResultsForTerm:@"fantastic" onComplete:^(NSDictionary *returnData, NSError *returnError) {
if ( returnError ) {
NSLog( @"Error getting data : %@", returnError.localizedDescription );
return;
}
//NSLog( @"return data : %@", returnData );
mydata = [returnData objectForKey:@"results"];
[mytable setData:mydata];
if ( myservice.isPreloadResult ) { return; }
[mytable.pullToRefresh stopAnimating];
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment