-
Notifications
You must be signed in to change notification settings - Fork 1
/
PullToRefreshTableViewController.m
118 lines (99 loc) · 3.61 KB
/
PullToRefreshTableViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// PullToRefreshTableViewController.m
// ASiST
//
// Created by Oliver on 09.12.09.
// Copyright 2009 Drobnik.com. All rights reserved.
//
#import "PullToRefreshTableViewController.h"
#define kReleaseToReloadStatus 0
#define kPullToReloadStatus 1
#define kLoadingStatus 2
@implementation PullToRefreshTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
refreshHeaderView = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - self.view.bounds.size.height, 320.0f, self.view.bounds.size.height)];
[self.tableView addSubview:refreshHeaderView];
self.tableView.showsVerticalScrollIndicator = YES;
}
- (void)dealloc {
[refreshHeaderView release];
[super dealloc];
}
#pragma mark State Changes
- (void)showReloadAnimationAnimated:(BOOL)animated {
reloading = YES;
[refreshHeaderView toggleActivityView:YES];
if (animated) {
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.2];
self.tableView.contentInset = UIEdgeInsetsMake(60.0f, 0.0f, 0.0f, 0.0f);
[UIView commitAnimations];
} else {
self.tableView.contentInset = UIEdgeInsetsMake(60.0f, 0.0f, 0.0f, 0.0f);
}
}
- (void)reloadTableViewDataSource {
NSLog(@"Please override reloadTableViewDataSource");
}
- (void)pullRefreshAnimated:(BOOL)animated {
[self.tableView setContentOffset:CGPointMake(0, -65.0f) animated:animated];
[self showReloadAnimationAnimated:NO];
[self reloadTableViewDataSource];
}
- (void)dataSourceDidFinishLoadingNewData {
reloading = NO;
[refreshHeaderView flipImageAnimated:NO];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:.3];
[self.tableView setContentInset:UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f)];
[refreshHeaderView setStatus:kPullToReloadStatus];
[refreshHeaderView toggleActivityView:NO];
[UIView commitAnimations];
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
return cell;
}
#pragma mark Scrolling Overrides
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
if (!reloading) {
checkForRefresh = YES; // only check offset when dragging
// refresh the pretty-printed last update date
refreshHeaderView.lastUpdatedDate = refreshHeaderView.lastUpdatedDate;
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (reloading) return;
if (checkForRefresh) {
if (refreshHeaderView.isFlipped && scrollView.contentOffset.y > -65.0f && scrollView.contentOffset.y < 0.0f && !reloading) {
[refreshHeaderView flipImageAnimated:YES];
[refreshHeaderView setStatus:kPullToReloadStatus];
} else if (!refreshHeaderView.isFlipped && scrollView.contentOffset.y < -65.0f) {
[refreshHeaderView flipImageAnimated:YES];
[refreshHeaderView setStatus:kReleaseToReloadStatus];
}
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
if (reloading) return;
if (scrollView.contentOffset.y <= - 65.0f) {
if ([self.tableView.dataSource respondsToSelector:@selector(reloadTableViewDataSource)]) {
[self showReloadAnimationAnimated:YES];
[self reloadTableViewDataSource];
}
}
checkForRefresh = NO;
}
@end