標籤:ios 網路
為練手寫了一個小的上拉載入更多下拉重新整理的小的Dome 。
沒有太多的技術含量,只是作為新手的啟發用。是上一篇下拉載入的擴充。先看一下那個再看這個就容易很多。
Dome下載:http://download.csdn.net/detail/u010123208/8062715
先來梳理一下:
上拉家在更多就是上拉之後在底部現實一個視圖,用來提示使用者上拉載入更多,如果使用者上拉就出發事件,進行載入並在試圖中給予提示,同時後台載入資料,將添加的資料加入資料數組,最後重新匯入列表;
下拉重新整理同樣的操作,只不過就是將數組清空重新來匯入資料;
首先我們要建立兩個view 一個是頂部顯示,一個在底部顯示 ;
在view中要顯示現在文字,一張圖片,一個活動指示框(當進行網路請求載入資料的時候顯示,其他時候隱藏),如果不太理解下載Dome看一下就知道了。
typedef enum { InsertStateNomal, //平常狀態 InsertStateDrapUp, //上拉狀態 InsertStateDrapDown, //下拉狀態 InsertStateRun, //正在載入的狀態}InsertState;@interface HeadView : UIView@property (nonatomic,strong) UIImageView *imageView; @property (nonatomic,strong) UILabel *label;@property (nonatomic,assign) InsertState insertState;@property (nonatomic,strong) UIActivityIndicatorView *activity;- (void)setInsertNomal; - (void)setInsertDrapDown;- (void)setInsertRun;@end@interface FootView : UIView@property (nonatomic,strong) UIImageView *imageView;@property (nonatomic,strong) UILabel *label;@property (nonatomic,assign) InsertState insertState;@property (nonatomic,strong) UIActivityIndicatorView *activity;- (void)setInsertNomal;- (void)setInsertDrapDoUp;- (void)setInsertRun;@end
枚舉用來指示視圖的目前狀態;
其實兩個視圖的內容什麼的都完全一樣;
//// InsertView.m// RefreshDome//// Created by 小屁孩 on 14-10-16.// Copyright (c) 2014年 XS. All rights reserved.//#import "InsertView.h"@implementation HeadView@synthesize imageView;@synthesize label;@synthesize insertState;@synthesize activity;- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor orangeColor]; //圖片 imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow.png"]]; imageView.frame = CGRectMake(20, frame.size.height-60, 30, 50); [self addSubview:imageView]; //顯示的文字 label = [[UILabel alloc]initWithFrame:CGRectMake(60, frame.size.height-60, 250, 50)]; label.text = @"下拉重新整理..."; label.textAlignment = NSTextAlignmentCenter; [self addSubview:label]; //狀態 insertState = InsertStateNomal; //活動指標 activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; activity.frame = CGRectMake(20,frame.size.height-60, 30, 50); [self addSubview:activity]; } return self;}//初始狀態-(void)setInsertNomal{ insertState = InsertStateNomal; label.text = @"下拉重新整理....."; imageView.layer.transform = CATransform3DMakeRotation(M_PI*2, 0, 0, 1); [activity stopAnimating]; imageView.hidden =NO;}//下拉狀態- (void)setInsertDrapDown{ insertState = InsertStateDrapDown; [UIView beginAnimations:nil context:nil]; label.text = @"釋放後重新整理....."; imageView.layer.transform = CATransform3DMakeRotation(M_PI, 0, 0, 1); [UIView commitAnimations]; }//重新整理狀態- (void)setInsertRun{ insertState =InsertStateRun; label.text = @"正在重新整理....."; imageView.hidden = YES; [activity startAnimating];}@end@implementation FootView@synthesize imageView;@synthesize label;@synthesize insertState;@synthesize activity;- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor orangeColor]; //圖片 imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"arrow.png"]]; imageView.layer.transform = CATransform3DMakeRotation(M_PI , 0, 0, 1); imageView.frame = CGRectMake(20, 10, 30, 50); [self addSubview:imageView]; //文字 label = [[UILabel alloc]initWithFrame:CGRectMake(60, 10, 250, 50)]; label.text = @"上拉載入更多....."; label.textAlignment = NSTextAlignmentCenter; [self addSubview:label]; //狀態 insertState = InsertStateNomal; //活動指標 activity = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; activity.frame = CGRectMake(20, 10, 30, 50); [self addSubview:activity]; } return self;}//初始狀態- (void)setInsertNomal{ insertState = InsertStateNomal; label.text = @"上拉載入更多....."; imageView.layer.transform = CATransform3DMakeRotation(M_PI , 0, 0, 1); [activity stopAnimating]; imageView.hidden =NO;}//上拉狀態- (void)setInsertDrapDoUp{ insertState = InsertStateDrapUp; [UIView beginAnimations:nil context:nil]; label.text = @"釋放後載入更多....."; imageView.layer.transform = CATransform3DMakeRotation(M_PI * 2, 0, 0, 1); [UIView commitAnimations];}//載入狀態- (void)setInsertRun{ insertState = InsertStateRun; label.text = @"正在載入....."; imageView.hidden = YES; [activity startAnimating];}@end
最後就是列表了;
其實在這裡面需要注意的:
- 要怎樣為表格添加視圖
- 添加的底部視圖隨著內容的增多,是不斷變化的(這裡我用了KVO來控制,當數組元素變化後處理位置)
- 當完成下拉或者上拉的時候,短暫的視圖顯示怎麼來控制
- 上拉下拉的判斷(UIScrollview代理)條件;
#import <UIKit/UIKit.h>#import "InsertView.h"@interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>@property (weak, nonatomic) IBOutlet UITableView *table;@property (nonatomic,strong) NSMutableArray *tableArray;@end
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController{ HeadView *headView; FootView *footView;}@synthesize tableArray;@synthesize table;- (void)viewDidLoad{ [super viewDidLoad]; table.frame = CGRectMake(0, 20, 320, [[UIScreen mainScreen]bounds].size.height-20); table.delegate = self; table.dataSource = self; table.tableFooterView = [[UIView alloc]init]; //下拉頭部視圖 headView = [[HeadView alloc]initWithFrame:CGRectMake(0, -251, DEVICE_FRAME.size.width, 251)]; [table addSubview:headView]; //上拉底部視圖 footView = [[FootView alloc]initWithFrame:CGRectMake(0, table.frame.size.height, DEVICE_FRAME.size.width, 251)]; [table addSubview:footView]; //初始化數組 tableArray = [NSMutableArray array]; for (int i = 0; i<15; i++) { NSURL *url=[NSURL URLWithString:@"http://www.sinaimg.cn/qc/photo_auto/chezhan/2012/50/00/15/80046_950.jpg"]; UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]]; [tableArray addObject:image]; } float hight =tableArray.count * 60 ; if (tableArray.count * 60 < [[UIScreen mainScreen]bounds].size.height ) { hight = table.frame.size.height; } footView.frame = CGRectMake(0, hight, 320, 251); [self addObserver:self forKeyPath:@"tableArray" options:NSKeyValueObservingOptionNew context:nil]; }- (void) addtableMutableArray{ for (int i = 0; i<5; i++) { NSURL *url=[NSURL URLWithString:@"http://www.sinaimg.cn/qc/photo_auto/chezhan/2012/50/00/15/80046_950.jpg"]; UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]]; [self.tableArray addObject:image]; } self.tableArray = tableArray; [footView setInsertNomal]; [self performSelectorOnMainThread:@selector(endThread) withObject:nil waitUntilDone:NO];}- (void)endThread{ [UIView beginAnimations:nil context:nil]; table.contentInset = UIEdgeInsetsMake(0, 0, 0, 0); [UIView commitAnimations]; [table reloadData]; NSLog(@"%d",tableArray.count);}- (void) refesh{ NSMutableArray *array = [NSMutableArray array]; for (int i = 0; i<15; i++) { NSURL *url=[NSURL URLWithString:@"http://wenwen.sogou.com/p/20110923/20110923201826-1347223277.jpg"]; UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]]; [array addObject:image]; } self.tableArray = array; [headView setInsertNomal]; [self performSelectorOnMainThread:@selector(endThread) withObject:nil waitUntilDone:NO];}#pragma mark KVO-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{ NSMutableArray *array = [change objectForKey:@"new"]; float hight =array.count * 60; if (array.count * 60 < [[UIScreen mainScreen]bounds].size.height ) { hight = [[UIScreen mainScreen]bounds].size.height; } footView.frame = CGRectMake(0, hight, 320, 251); }#pragma mark tabledelegate- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return tableArray.count;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 60;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *identityCell = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identityCell]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identityCell]; } cell.imageView.image = [tableArray objectAtIndex:indexPath.row]; cell.textLabel.text = @"NibBi"; return cell;}#pragma mark - scrolldelegae- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{ if (headView.insertState == InsertStateDrapDown) { [UIView beginAnimations:nil context:nil]; table.contentInset = UIEdgeInsetsMake(70, 0, 0, 0); [headView setInsertRun]; [UIView commitAnimations]; [self performSelectorInBackground:@selector(refesh) withObject:nil]; } if (footView.insertState == InsertStateDrapUp) { [UIView beginAnimations:nil context:nil]; table.contentInset = UIEdgeInsetsMake(0, 0, 70, 0); [footView setInsertRun]; [UIView commitAnimations]; [self performSelectorInBackground:@selector(addtableMutableArray) withObject:nil]; }}-(void)scrollViewDidScroll:(UIScrollView *)scrollView{ // NSLog(@"%f",scrollView.contentOffset.y); //上拉載入更多轉換 if (scrollView.contentOffset.y > scrollView.contentSize.height - scrollView.frame.size.height + 60 && footView.insertState == InsertStateNomal) { [UIView beginAnimations:nil context:nil]; [footView setInsertDrapDoUp]; [UIView commitAnimations]; } //下拉重新整理轉換 if (scrollView.contentOffset.y < -60 && headView.insertState == InsertStateNomal) { [UIView beginAnimations:nil context:nil]; [headView setInsertDrapDown]; [UIView commitAnimations]; }}@end
ios 上拉載入下拉重新整理Dome