IOS學習之UiTableView下拉刷新與自動加載更多,百年不變的效果(五)
來源:程序員人生 發布時間:2015-07-30 14:35:25 閱讀次數:4909次
51勞動節馬上來臨,小火伴有妹有很激動喲,首先祝天下所有的程序猿節日快樂!這個51對我來講有點不1樣,我的人生從這個51就轉彎了,愛情長跑8年的我結婚了,1會支付寶賬號我會公布出去,請自覺打款!謝謝合作。
燈光閃起來:

舞蹈跳起來:

歌曲唱起來:
----------------------------------------------------------------------------------------------------------------------------------------------------------
智能世界的里面,做多的是甚么?對,是APP,遮天蔽日的APP隨之襲來,APP死亡潮也隨之出現,我們生活在互聯網的時期里,每一個人都是產品經理,聽說,我們每天用的APP不超過8個,反正我每天必用百度云,你曉得,雖然APP多,但是他們都有1個共同點,你們知道是甚么?你們千萬不要告知我是用手機玩的,他們就是都下拉刷新和加載更多,這些產品經理所有的共同點,雖然下拉刷新的動畫都不1樣,但是他們的目的只有個,就是獲得服務器最最新的數據,我們都知道Android里面有很多開源的下拉刷新的第3方比如著名的:Android-PullToRefresh。這個開源的很強大啊,支持很多控件,ListView、ViewPager、WevView、ExpandableListView、GridView、(Horizontal
)ScrollView、Fragment 點擊下載,固然你也能夠自己寫!既然是土豪的玩的IOS,怎樣可能少了第3方的下拉刷新:但是比較著名的就是:EGOTableViewPullRefresh跟MJRefresh點擊下載1------------點擊下載2。我自認為上拉加載更多對用戶簡直是1直折磨,翻到最后1頁,居然還要上拉1下才能加載更多。1點也不暢,但是我感覺自動加載更多比較人性化,感覺很順!自己的觀點而已!下面我是使MJRefresh這個來學習的,自己集成的自動加載更多,下面來效果圖:

我們發現我們不需要上拉就能夠自動加載更多,我感覺這類用戶體驗比較好:
繼承步驟:
1:首先我們需要把MJRefresh這個開源的項目下載,把里面
全部考進自己的項目中,要先把這個文件拷貝到自己的項目的目錄中,在xcode上右鍵add file這個文件我們才能把這個所需要的文件考進來,
2:實現:直接看代碼:
//
// ViewController.m
// MyUitableViewRefreshAndMore
//
// Created by xiaoyuan on 15/4/28.
// Copyright (c) 2015年 xiaoyuan. All rights reserved.
//
#import "MyUitableViewRefreshViewController.h"
#import "MJRefresh.h"
#import "DataSingleton.h"
static const CGFloat MJDuration = 1.0;
//#define MJRandomData [NSString stringWithFormat:@"隨機數據---%d", arc4random_uniform(1000000)]
#define DISPATCH_TIME_NOW (0ull)
#define NSEC_PER_SEC 1000000000ull
@interface MyUitableViewRefreshViewController ()<UITableViewDelegate,UITableViewDataSource>
{
UITableView *table;
NSMutableArray *data;
//判斷是不是加載完成
BOOL isLoadIOver;
}
@end
@implementation MyUitableViewRefreshViewController
- (void)viewDidLoad {
[super viewDidLoad];
self .view .backgroundColor = [UIColor whiteColor];
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
UILabel*title = [[UILabel alloc] initWithFrame:CGRectZero];
title.text = @"刷新吧小宇宙";
title.backgroundColor =[UIColor clearColor];
title.textColor = [UIColor whiteColor];
self.navigationItem.titleView = title;
[title sizeToFit];
[self.navigationController.navigationBar setTranslucent:NO];
__weak typeof(self) weakSelf = self;
if(data == nil){
data = [[NSMutableArray alloc] init];
}
self.view.backgroundColor = [UIColor whiteColor];
table = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds];
table.backgroundColor =[UIColor whiteColor];
table.separatorStyle = UITableViewCellEditingStyleNone;
table.delegate =self;
table.dataSource =self;
[self.view addSubview:table];
[table addLegendHeaderWithRefreshingBlock:^{
[weakSelf loadNewData :YES];
}];
// 進入自動刷新
[table.legendHeader beginRefreshing];
}
- (void)loadNewData:(BOOL)reresh
{
isLoadIOver = NO;
//刷新移除所有數據
if(reresh){
[data removeAllObjects];
}
// 1.添加假數據
for (int i = 0; i<20; i++) {
[data addObject:@(i)];
}
// 2.摹擬2秒后刷新表格UI(真實開發中,可以移除這段gcd代碼)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 刷新表格
[table reloadData];
isLoadIOver = YES;
// 拿到當前的下拉刷新控件,結束刷新狀態
[table.header endRefreshing];
});
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//只要大于的數是小于第1次返回的個數就行
return data.count > 10? data.count + 1 : data.count;
}
//
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 44;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if([data count]>0){
if(indexPath.row<[data count]){
static NSString *CellIdentifier =@"Cell";
UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[ UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//避免點擊把分割線弄掉
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//這個千萬要記住寫小于44,由于系統默許的就是44 ,這個把我坑壞了,1直找不到緣由,1直以為重用的緣由
//大于44就超越高度,系統就給回收掉了
UIView*view= [[UIView alloc] initWithFrame:CGRectMake(0, 43, 400, 1)];
view.backgroundColor = [UIColor grayColor];
[cell addSubview:view];
}
cell.textLabel.text = [NSString stringWithFormat:@"%@",[data objectAtIndex:indexPath.row]];
cell .textLabel.textAlignment = NSTextAlignmentCenter;
return cell;
}
}
//加載更多
if(isLoadIOver){
[self loadNewData:NO];
}
return [DataSingleton.Instance getLoadMoreCell:table andIsLoadOver:NO andLoadOverString:@"正在加載..." andLoadingString:(YES ? @"正在加載" : @"下面10個") andIsLoading:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
DataSingleton類:
//
// DataSingleton.h
// MyUiTableRefresh
//
// Created by xiaoyuan on 15/4/29.
// Copyright (c) 2015年 xiaoyuan. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "LoadingCell.h"
@interface DataSingleton : NSObject
#pragma 單例模式
+ (DataSingleton *) Instance;
+ (id)allocWithZone:(NSZone *)zone;
//返回標示正在加載的選項
- (UITableViewCell *)getLoadMoreCell:(UITableView *)tableView
andIsLoadOver:(BOOL)isLoadOver
andLoadOverString:(NSString *)loadOverString
andLoadingString:(NSString *)loadingString
andIsLoading:(BOOL)isLoading;
@end
//
// DataSingleton.m
// MyUiTableRefresh
//
// Created by xiaoyuan on 15/4/29.
// Copyright (c) 2015年 xiaoyuan. All rights reserved.
//
#import "DataSingleton.h"
#import <QuartzCore/QuartzCore.h>
@implementation DataSingleton
- (UITableViewCell *)getLoadMoreCell:(UITableView *)tableView
andIsLoadOver:(BOOL)isLoadOver
andLoadOverString:(NSString *)loadOverString
andLoadingString:(NSString *)loadingString
andIsLoading:(BOOL)isLoading
{
LoadingCell * cell = [tableView dequeueReusableCellWithIdentifier:@"loadingCell"];
if (!cell) {
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:@"LoadingCell" owner:self options:nil];
for (NSObject *o in objects) {
if ([o isKindOfClass:[LoadingCell class]]) {
cell = (LoadingCell *)o;
break;
}
}
}
cell.lbl.font = [UIFont boldSystemFontOfSize:15 - 2];
cell.lbl.text = isLoadOver ? loadOverString : loadingString;
if (isLoading) {
cell.loading.hidden = NO;
[cell.loading startAnimating];
}
else
{
cell.loading.hidden = YES;
[cell.loading stopAnimating];
}
return cell;
}
#pragma 單例模式定義
static DataSingleton * instance = nil;
+(DataSingleton *) Instance
{
if(nil == instance) {
@synchronized(self) {
if(nil == instance) {
instance = [[DataSingleton alloc] init];
}
}
}
return instance;
}
//-(void) dealloc
//{
// [instance release];
// [super dealloc];
//}
+(id)allocWithZone:(NSZone *)zone
{
@synchronized(self)
{
if(instance == nil)
{
instance = [super allocWithZone:zone];
return instance;
}
}
return nil;
}
@end
主要代碼就是這些,還有1個LoadingCell類,就不貼了,這主要是MJReresh得基礎上加上自動加載,固然里面還有1些邏輯需要待開發的,比如網絡的緣由了,還有數據全部加載完成了,這些就看需求來改!最后感謝李明杰老師,開源了這么優秀的代碼。祝大家51快樂,東北走起,求偶遇!
代碼下載
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈