master
1//
2// SVViewController.m
3// SVPullToRefreshDemo
4//
5// Created by Sam Vermette on 23.04.12.
6// Copyright (c) 2012 Home. All rights reserved.
7//
8
9#import "SVViewController.h"
10#import "SVPullToRefresh.h"
11
12@interface SVViewController () <UITableViewDelegate, UITableViewDataSource>
13
14@property (nonatomic, strong) NSMutableArray *dataSource;
15
16@end
17
18@implementation SVViewController
19@synthesize tableView = tableView;
20
21- (void)viewDidLoad {
22 [super viewDidLoad];
23
24 self.dataSource = [NSMutableArray array];
25
26 for(int i=0; i<15; i++)
27 [self.dataSource addObject:[NSDate dateWithTimeIntervalSinceNow:-(i*90)]];
28
29 __weak SVViewController *weakSelf = self;
30
31 // setup pull-to-refresh
32 [self.tableView addPullToRefreshWithActionHandler:^{
33
34 int64_t delayInSeconds = 2.0;
35 dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
36 dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
37 [weakSelf.tableView beginUpdates];
38 [weakSelf.dataSource insertObject:[NSDate date] atIndex:0];
39 [weakSelf.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationBottom];
40 [weakSelf.tableView endUpdates];
41
42 [weakSelf.tableView.pullToRefreshView stopAnimating];
43 });
44 }];
45
46 // setup infinite scrolling
47 [self.tableView addInfiniteScrollingWithActionHandler:^{
48
49 int64_t delayInSeconds = 2.0;
50 dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
51 dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
52 [weakSelf.tableView beginUpdates];
53 [weakSelf.dataSource addObject:[weakSelf.dataSource.lastObject dateByAddingTimeInterval:-90]];
54 [weakSelf.tableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:weakSelf.dataSource.count-1 inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
55 [weakSelf.tableView endUpdates];
56
57 [weakSelf.tableView.infiniteScrollingView stopAnimating];
58 });
59 }];
60
61 // trigger the refresh manually at the end of viewDidLoad
62 [tableView triggerPullToRefresh];
63}
64
65#pragma mark -
66#pragma mark UITableViewDataSource
67
68- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
69 return 1;
70}
71
72- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
73 return self.dataSource.count;
74}
75
76- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
77 static NSString *identifier = @"Cell";
78 UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:identifier];
79
80 if (cell == nil)
81 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
82
83 NSDate *date = [self.dataSource objectAtIndex:indexPath.row];
84 cell.textLabel.text = [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterNoStyle timeStyle:NSDateFormatterMediumStyle];
85 return cell;
86}
87
88@end