Pull to Refresh in TableView
What I am going to do is.....
Step 1
create previous project
Step 2
--------------------------do the changes to ViewController.h class......................
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
IBOutlet UITableView
*profileTableView;
NSMutableArray
*profileImageArray;
NSMutableArray *nameArray;
NSMutableArray
*companyNameArray;
BOOL isDragging;
BOOL isLoading;
}
@property(strong,nonatomic) IBOutlet UITableView *profileTableView;
@property(strong,nonatomic) NSMutableArray *profileImageArray;
@property(strong,nonatomic) NSMutableArray *nameArray;
@property(strong,nonatomic) NSMutableArray *companyNameArray;
@property (nonatomic, retain) UIView *refreshHeaderView;
@property (nonatomic, retain) UILabel *refreshLabel;
@property (nonatomic, retain) UIImageView *refreshArrow;
@property (nonatomic, retain) UIActivityIndicatorView *refreshSpinner;
@property (nonatomic, copy) NSString *textPull;
@property (nonatomic, copy) NSString *textRelease;
@property (nonatomic, copy) NSString *textLoading;
- (void)setupStrings;
- (void)addPullToRefreshHeader;
- (void)startLoading;
- (void)stopLoading;
- (void)refresh;
@end
Step 3
---------------------------do the changes to ViewController.m class......................
#import "ViewController.h"
#import "CustomCell.h"
#define BUTTON_LEFT_MARGIN 10.0
#define BUTTON_SPACING 25.0
#define PUSH_STYLE_ANIMATION NO
#define REFRESH_HEADER_HEIGHT 52.0f
#define USE_GESTURE_RECOGNIZERS YES
// Bounce pixels define how many pixels the view is moved during
the bounce animation
#define BOUNCE_PIXELS 5.0
@interface ViewController ()
@end
@implementation ViewController
@synthesize profileTableView,profileImageArray,companyNameArray,nameArray;
@synthesize textPull, textRelease, textLoading,
refreshHeaderView, refreshLabel, refreshArrow, refreshSpinner;
#pragma mark - Life Cycle
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle
*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom
initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any
additional setup after loading the view from its nib.
[self setupStrings];
[self addPullToRefreshHeader];
profileImageArray =[[NSMutableArray alloc]initWithObjects:
@"http://upload.wikimedia.org/wikipedia/commons/e/eb/SLIIT_MLB_1.jpg",
@"https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGRrSBTxhJEVFgjD-bFIW8Qpqj4XFg4lk7B9J-KiPwdPTJTtHvBnZVKGUECOZc3ojXSj0ksd_jgkL5_fPd0MEYYVlNrQEccBtnvb8yRKQLsiYYXyh_qADCZjO9XJXMxv4_QVPNaMOYnEOp/s220/nwmpic.jpg",
@"http://www.userlogos.org/files/logos/robinhood/sliit400.png",
@"https://lh5.googleusercontent.com/-c6f2H9sAeDM/AAAAAAAAAAI/AAAAAAAAATM/9hzjaMbm4dM/photo.jpg",
@"https://lh4.googleusercontent.com/-qGRFN6zAkwA/AAAAAAAAAAI/AAAAAAAAAAA/2vec90BT9UQ/photo.jpg",
@"http://www.imagesbuddy.com/images/132/2014/01/purple-teddy-bear-happy-teddy-bear-day-2014.jpg",
@"https://lh3.googleusercontent.com/-8MeJ9g6xRso/AAAAAAAAAAI/AAAAAAAAAAA/1A6e1NaLimU/photo.jpg",
@"https://lh4.googleusercontent.com/-yaYTiufsV6I/AAAAAAAAAAI/AAAAAAAAAAA/TLDDaIWQ-Sk/photo.jpg",
@"http://www.interviewmagazine.com/files/2011/06/29/img-simon-van-booy_151144210967.png",
@"http://m.c.lnkd.licdn.com/mpr/pub/image-Dnol_IS4AkICXerqwCXULDwKFbuqEAnT7r_bL7l_Ftusk7LmDnobMsk4F33qksdVEyhq/ishara-sunjeewa.jpg",
nil];
nameArray =[[NSMutableArray alloc]initWithObjects:
@"Dhanushka
Adrian",@"Dinithe",@"Nilakshan",@"Niron",@"Jude",
@"Malsha",@"Kasun",@"Thisra",@"Suranga",@"Ishara",
nil];
companyNameArray =[[NSMutableArray alloc]initWithObjects:
@"Vburst Software",@"Vburst
Software",@"Vburst Software",@"EZCrM",
@"Chello
Dairy Products(Pvt)Ltd",@"Vburst Software",@"Vburst
Software",
@"Vburst
Software",@"Vburst Software",@"Vburst Software",
nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose
of any resources that can be recreated.
}
#pragma mark - Table Methods
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
int nodeCount = [nameArray count];
if (nodeCount ==
0){
return 1;
}
else{
NSLog(@"%d",nodeCount);
return nodeCount;
}
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"cellForRowAtIndexPath");
NSString
*uniqueIdentifier = @"CustomCell";
CustomCell *cell = nil;
cell = (CustomCell *) [profileTableView dequeueReusableCellWithIdentifier:uniqueIdentifier];
if(!cell)
{
NSArray
*topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:nil];
for(id currentObject
in
topLevelObjects)
{
if([currentObject
isKindOfClass:[CustomCell class]])
{
cell = (CustomCell
*)currentObject;
break;
}
}
}
cell.nameLabel.text = [nameArray objectAtIndex:indexPath.row];
cell.companyLabel.text = [companyNameArray objectAtIndex:indexPath.row];
NSString *AllPicUrl=[profileImageArray objectAtIndex:indexPath.row];
//image
place holder---------------
[cell.profileImageView setImage:[UIImage imageNamed:@"defaultImgae.jpeg"]];
//Lazy
Loading------------------------------------------
dispatch_queue_t imageQueue = dispatch_queue_create("imageDownloder", nil);
dispatch_async(imageQueue,
^{
//This is
what you will load lazily
NSURL *imageUrl=[NSURL URLWithString:AllPicUrl];
NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
dispatch_sync(dispatch_get_main_queue(), ^{
cell.profileImageView.image = [UIImage imageWithData:imageData];;
[cell setNeedsLayout];
});
});
//--------------------------------------------------------
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 90.0f;
}
- (void)reloadTheTableView{
[self.profileTableView reloadData];
}
#pragma mark - Pull To Reload Method
- (void)refresh {
[self performSelector:@selector(addItem) withObject:nil afterDelay:2.0];
}
- (void)addItem {
[self reloadTheTableView]; //for this i call the reload table view method
[self stopLoading];
}
- (void)setupStrings{
textPull = @"Pull
down to refresh...";
textRelease = @"Release
to refresh...";
textLoading = @"Loading...";
}
- (void)addPullToRefreshHeader {
refreshHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0 - REFRESH_HEADER_HEIGHT, 320, REFRESH_HEADER_HEIGHT)];
refreshHeaderView.backgroundColor = [UIColor clearColor];
refreshLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, REFRESH_HEADER_HEIGHT)];
refreshLabel.backgroundColor = [UIColor clearColor];
refreshLabel.font = [UIFont boldSystemFontOfSize:12.0];
refreshLabel.textAlignment = NSTextAlignmentCenter;
[refreshLabel setTextColor:[UIColor blackColor]];
refreshArrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"arrow.png"]];
refreshArrow.frame = CGRectMake(floorf((REFRESH_HEADER_HEIGHT - 27) / 2),
(floorf(REFRESH_HEADER_HEIGHT - 44) / 2),
27, 44);
refreshSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
refreshSpinner.frame = CGRectMake(floorf(floorf(REFRESH_HEADER_HEIGHT - 20) / 2), floorf((REFRESH_HEADER_HEIGHT - 20) / 2), 20, 20);
refreshSpinner.hidesWhenStopped = YES;
[refreshHeaderView addSubview:refreshLabel];
[refreshHeaderView addSubview:refreshArrow];
[refreshHeaderView addSubview:refreshSpinner];
[self.profileTableView addSubview:refreshHeaderView];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (isLoading) {
if (scrollView.contentOffset.y > 0)
self.profileTableView.contentInset = UIEdgeInsetsZero;
else if (scrollView.contentOffset.y >= -REFRESH_HEADER_HEIGHT)
self.profileTableView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
} else if (isDragging &&
scrollView.contentOffset.y < 0) {
[UIView animateWithDuration:0.25 animations:^{
if (scrollView.contentOffset.y < -REFRESH_HEADER_HEIGHT) {
refreshLabel.text = self.textRelease;
[refreshArrow layer].transform = CATransform3DMakeRotation(M_PI, 0, 0, 1);
} else {
refreshLabel.text = self.textPull;
[refreshArrow layer].transform = CATransform3DMakeRotation(M_PI * 2, 0, 0, 1);
}
}];
}
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView
willDecelerate:(BOOL)decelerate {
if (isLoading) return;
isDragging = NO;
if (scrollView.contentOffset.y <= -REFRESH_HEADER_HEIGHT) {
[self startLoading];
}
}
- (void)startLoading {
isLoading = YES;
[UIView animateWithDuration:0.3 animations:^{
self.profileTableView.contentInset = UIEdgeInsetsMake(REFRESH_HEADER_HEIGHT, 0, 0, 0);
refreshLabel.text = self.textLoading;
refreshArrow.hidden = YES;
[refreshSpinner startAnimating];
}];
[self refresh];
}
- (void)stopLoading {
isLoading = NO;
[UIView animateWithDuration:0.3 animations:^{
self.profileTableView.contentInset = UIEdgeInsetsZero;
[refreshArrow layer].transform = CATransform3DMakeRotation(M_PI * 2, 0, 0, 1);
}
completion:^(BOOL finished) {
[self performSelector:@selector(stopLoadingComplete)];
}];
}
- (void)stopLoadingComplete {
refreshLabel.text = self.textPull;
refreshArrow.hidden = NO;
[refreshSpinner stopAnimating];
}
@end
Good Luck!
- SQLite Based iPhone Application
- json with IOS (Basic) - Tutorial
- Simple Table View in IOS
- Load Distinct Images from array into UITableView
- Simple UIPickerView Example
- Tab Bar Application Part 1
- Tab Bar Application Part 2
- Tab Bar Application Part 3 ( with Login Screen )
- Tab Bar Application Part 4 ( Navigation )
- Table View Design in IOS Part 1
- Table View Design in IOS Part 2 ( Time Table )
- How to Add search bar in Table View
- Local Notification in IOS
- Core Data (For Beginners )
- Core Data Tute 2 (Add/Delete/Search)
- Core Data Tute 3 (Two table)
- Custom Cell in UITableview
- Lazy Loading
- Pull to Refresh in TableView
- Working with keyboard in Objective C - Part 1