iOS开发UI篇-在UItableview中实现加载更多功能
iOS開發(fā)UI篇-在UItableview中實(shí)現(xiàn)加載更多功能
一、實(shí)現(xiàn)效果
?
點(diǎn)擊加載更多按鈕,出現(xiàn)一個(gè)加載圖示,三秒鐘后添加兩條新的數(shù)據(jù)。
?
二、實(shí)現(xiàn)代碼和說明
當(dāng)在頁面(視圖部分)點(diǎn)擊加載更多按鈕的時(shí)候,主頁面(主控制器)會(huì)加載兩條數(shù)據(jù)進(jìn)來。
視圖部分的按鈕被點(diǎn)擊的時(shí)候,要讓主控制器加載數(shù)據(jù),刷新表格,2B青年會(huì)在視圖中增加一個(gè)主控制器的屬性,通過這個(gè)屬性去調(diào)用進(jìn)行加載,但在開發(fā)中通常通過代理模式來完成這個(gè)操作。
下面分別是兩種實(shí)現(xiàn)的代碼。
1、項(xiàng)目結(jié)構(gòu)和說明
說明:加載更多永遠(yuǎn)都放在這個(gè)tableview的最下端,因此這里設(shè)置成了這個(gè)tableview的tableFooterView。
?self.tableview.tableFooterView=footerview;
在實(shí)現(xiàn)上通過xib來進(jìn)行處理,考慮到左右的留白,以及點(diǎn)擊后的要切換到加載按鈕和文字,要同時(shí)控制圖標(biāo)和文字,因此把加載圖標(biāo)和文字提示放在了一個(gè)view中以便控制,這個(gè)xib已經(jīng)和YYfooterview.xib進(jìn)行了關(guān)聯(lián),通過這個(gè)類來控制xib。
2、實(shí)現(xiàn)代碼
(1).垃圾代碼
數(shù)據(jù)模型部分
YYtg.h文件
// // YYtg.h // 02-團(tuán)購(使用xib和類完成數(shù)據(jù)展示) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. All rights reserved. // #import <Foundation/Foundation.h> #import "Global.h"@interface YYtgModel : NSObject @property(nonatomic,copy)NSString *icon; @property(nonatomic,copy)NSString *buyCount; @property(nonatomic,copy)NSString *title; @property(nonatomic,copy)NSString *price;//對(duì)外接口 YYinitH(tg) @endYYtg.m文件
// // YYtg.m // 02-團(tuán)購(使用xib和類完成數(shù)據(jù)展示) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. All rights reserved. // #import "YYtgModel.h"@implementation YYtgModel YYinitM(tg) @end注意:對(duì)于數(shù)據(jù)轉(zhuǎn)模型部分的構(gòu)造方法接口和實(shí)現(xiàn)代碼已經(jīng)通過自定義帶參數(shù)的宏來進(jìn)行了封裝。
封裝代碼如下:
#ifndef _0____________Global_h #define _0____________Global_h/*** 自定義帶參數(shù)的宏*/ #define YYinitH(name) -(instancetype)initWithDict:(NSDictionary *)dict;\ +(instancetype)name##WithDict:(NSDictionary *)dict;#define YYinitM(name) -(instancetype)initWithDict:(NSDictionary *)dict\ {\if (self=[super init]) {\[self setValuesForKeysWithDictionary:dict];\}\return self;\ }\ \ +(instancetype)name##WithDict:(NSDictionary *)dict\ {\return [[self alloc]initWithDict:dict];\ }\#endif視圖部分
YYtgcell.h文件
// // YYtgcell.h // 02-團(tuán)購(使用xib和類完成數(shù)據(jù)展示) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. All rights reserved. // #import <UIKit/UIKit.h> #import "YYtgModel.h"@interface YYtgcell : UITableViewCell @property(nonatomic,strong)YYtgModel *yytg;//把加載數(shù)據(jù)(使用xib創(chuàng)建cell的內(nèi)部細(xì)節(jié)進(jìn)行封裝) +(instancetype)tgcellWithTableView:(UITableView *)tableView; @endYYtgcell.m文件
// // YYtgcell.m // 02-團(tuán)購(使用xib和類完成數(shù)據(jù)展示) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. All rights reserved. // #import "YYtgcell.h" //私有擴(kuò)展 @interface YYtgcell() @property (strong, nonatomic) IBOutlet UIImageView *img; @property (strong, nonatomic) IBOutlet UILabel *titlelab; @property (strong, nonatomic) IBOutlet UILabel *pricelab; @property (strong, nonatomic) IBOutlet UILabel *buycountlab; @end @implementation YYtgcell#pragma mark 重寫set方法,完成數(shù)據(jù)的賦值操作 -(void)setYytg:(YYtgModel *)yytg {_yytg=yytg;self.img.image=[UIImage imageNamed:yytg.icon];self.titlelab.text=yytg.title;self.pricelab.text=[NSString stringWithFormat:@"$%@",yytg.price];self.buycountlab.text=[NSString stringWithFormat:@"已有%@人購買",yytg.buyCount]; }+(instancetype)tgcellWithTableView:(UITableView *)tableView {static NSString *identifier= @"tg";YYtgcell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];if (cell==nil) {//如何讓創(chuàng)建的cell加個(gè)戳//對(duì)于加載的xib文件,可以到xib視圖的屬性選擇器中進(jìn)行設(shè)置cell=[[[NSBundle mainBundle]loadNibNamed:@"tgcell" owner:nil options:nil]firstObject];NSLog(@"創(chuàng)建了一個(gè)cell");}return cell; }@endYYfooterview.h文件
// // YYfooterview.h // 02-團(tuán)購(使用xib和類完成數(shù)據(jù)展示) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. All rights reserved. // #import <UIKit/UIKit.h> @class YYViewController; @interface YYfooterview : UIView @property(nonatomic,strong) YYViewController *controller; @endYYfooterview.m文件
// // YYfooterview.m // 02-團(tuán)購(使用xib和類完成數(shù)據(jù)展示) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. All rights reserved. // #import "YYfooterview.h" #import "YYViewController.h"@interface YYfooterview () @property (strong, nonatomic) IBOutlet UIActivityIndicatorView *loadingview; @property (strong, nonatomic) IBOutlet UIButton *loadbtn;@end @implementation YYfooterview - (IBAction)loadbtclick {NSLog(@"按鈕被點(diǎn)擊了");//隱藏按鈕self.loadbtn.hidden=YES;//顯示菊花self.loadingview.hidden=NO;#warning 模擬發(fā)送網(wǎng)絡(luò)請(qǐng)求, 3秒之后隱藏菊花dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{// 3.調(diào)用控制的加載數(shù)據(jù)方法 [self.controller LoadMore];// 4.隱藏菊花視圖self.loadingview.hidden = YES;// 5.顯示按鈕self.loadbtn.hidden = NO;}); }@end主控制器
YYViewController.h文件
// // YYViewController.h // 02-團(tuán)購(使用xib和類完成數(shù)據(jù)展示) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. All rights reserved. // #import <UIKit/UIKit.h>@interface YYViewController : UIViewController //公開接口 //- (void)LoadMore; @endYYViewController.m文件
// // YYViewController.m // 02-團(tuán)購(使用xib和類完成數(shù)據(jù)展示) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. All rights reserved. // #import "YYViewController.h" #import "YYtgModel.h" #import "YYtgcell.h" #import "YYfooterview.h"@interface YYViewController ()<UITableViewDataSource,UITableViewDelegate> @property (strong, nonatomic) IBOutlet UITableView *tableview;@property(strong,nonatomic)NSMutableArray *tg; @end@implementation YYViewController#pragma mark-加載數(shù)據(jù)方法 -(void)LoadMore {//創(chuàng)建模型YYtgModel *tgmodel=[[YYtgModel alloc]init];tgmodel.title=@"菜好上桌";tgmodel.icon=@"5ee372ff039073317a49af5442748071";tgmodel.buyCount=@"20";tgmodel.price=@"10000";//將模型添加到數(shù)組中 [self.tg addObject:tgmodel];YYtgModel *tgmodelq=[[YYtgModel alloc]init];tgmodelq.title=@"菜好上桌1";tgmodelq.icon=@"5ee372ff039073317a49af5442748071";tgmodelq.buyCount=@"20";tgmodelq.price=@"10000";[self.tg addObject:tgmodelq];//刷新表格 [self.tableview reloadData]; }- (void)viewDidLoad {[super viewDidLoad];self.tableview.rowHeight=80.f;//加載底部視圖//從xib中獲取數(shù)據(jù)UINib *nib=[UINib nibWithNibName:@"YYfooterview" bundle:nil];YYfooterview *footerview=[[nib instantiateWithOwner:nil options:nil] firstObject];self.tableview.tableFooterView=footerview;//設(shè)置控制footerview.controller=self; } #pragma mark- 懶加載 -(NSArray *)tg {if (_tg==nil) {NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"tgs.plist" ofType:nil];NSArray *temparray=[NSArray arrayWithContentsOfFile:fullpath];NSMutableArray *arrayM=[NSMutableArray arrayWithCapacity:temparray.count];for (NSDictionary *dict in temparray) {YYtgModel *tg=[YYtgModel tgWithDict:dict];[arrayM addObject:tg];}_tg=arrayM;}return _tg; }#pragma mark- xib創(chuàng)建cell數(shù)據(jù)處理#pragma mark 多少組 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 1; }#pragma mark多少行 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return self.tg.count; }#pragma mark設(shè)置每組每行 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {//1.創(chuàng)建cellYYtgcell *cell=[YYtgcell tgcellWithTableView:tableView];//2.獲取當(dāng)前行的模型,設(shè)置cell的數(shù)據(jù)YYtgModel *tg=self.tg[indexPath.row];cell.yytg=tg;//3.返回cellreturn cell; }#pragma mark- 隱藏狀態(tài)欄 -(BOOL)prefersStatusBarHidden {return YES; }@end2.通過代理完成
當(dāng)按鈕被點(diǎn)擊的時(shí)候,視圖部分本身不干活,而是通知它的代理(控制器)完成接下來的操作。
該部分代碼在1的基礎(chǔ)上對(duì)下面幾個(gè)文件進(jìn)行了修改:
視圖部分:
YYfooterview.h文件
// // YYfooterview.h // 02-團(tuán)購(使用xib和類完成數(shù)據(jù)展示) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. All rights reserved. // #import <UIKit/UIKit.h> @class YYViewController ,YYfooterview; //約定協(xié)議 @protocol YYfooterviewDelegate <NSObject> -(void)footerviewLoadMore; @end@interface YYfooterview : UIView//聲明一個(gè)id類型屬性,遵守了協(xié)議的“人”即可成為它的代理 @property(nonatomic,strong)id<YYfooterviewDelegate> delegate; //@property(nonatomic,strong) YYViewController *controller; @endYYfooterview.m文件
// // YYfooterview.m // 02-團(tuán)購(使用xib和類完成數(shù)據(jù)展示) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. All rights reserved. // #import "YYfooterview.h" #import "YYViewController.h"@interface YYfooterview () @property (strong, nonatomic) IBOutlet UIActivityIndicatorView *loadingview; @property (strong, nonatomic) IBOutlet UIButton *loadbtn;@end @implementation YYfooterview - (IBAction)loadbtclick {NSLog(@"按鈕被點(diǎn)擊了");//隱藏按鈕self.loadbtn.hidden=YES;//顯示菊花self.loadingview.hidden=NO;#warning 模擬發(fā)送網(wǎng)絡(luò)請(qǐng)求, 3秒之后隱藏菊花dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{// 3.調(diào)用控制的加載數(shù)據(jù)方法 // [self.controller LoadMore];//通知代理(需要進(jìn)行判斷,代理有沒有實(shí)現(xiàn)相應(yīng)的方法)[self.delegate footerviewLoadMore];// 4.隱藏菊花視圖self.loadingview.hidden = YES;// 5.顯示按鈕self.loadbtn.hidden = NO;}); }@end主控制器部分
YYViewController.h文件
// // YYViewController.h // 02-團(tuán)購(使用xib和類完成數(shù)據(jù)展示) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. All rights reserved. // #import <UIKit/UIKit.h>@interface YYViewController : UIViewController //公開接口 //- (void)LoadMore; @endYYViewController.m文件
// // YYViewController.m // 02-團(tuán)購(使用xib和類完成數(shù)據(jù)展示) // // Created by apple on 14-5-29. // Copyright (c) 2014年 itcase. All rights reserved. // #import "YYViewController.h" #import "YYtgModel.h" #import "YYtgcell.h" #import "YYfooterview.h"@interface YYViewController ()<UITableViewDataSource,UITableViewDelegate,YYfooterviewDelegate> @property (strong, nonatomic) IBOutlet UITableView *tableview;@property(strong,nonatomic)NSMutableArray *tg; @end@implementation YYViewController#pragma mark-加載數(shù)據(jù)方法 -(void)footerviewLoadMore {//創(chuàng)建模型YYtgModel *tgmodel=[[YYtgModel alloc]init];tgmodel.title=@"菜好上桌";tgmodel.icon=@"5ee372ff039073317a49af5442748071";tgmodel.buyCount=@"20";tgmodel.price=@"10000";//將模型添加到數(shù)組中 [self.tg addObject:tgmodel];YYtgModel *tgmodelq=[[YYtgModel alloc]init];tgmodelq.title=@"菜好上桌1";tgmodelq.icon=@"5ee372ff039073317a49af5442748071";tgmodelq.buyCount=@"20";tgmodelq.price=@"10000";[self.tg addObject:tgmodelq];//刷新表格 [self.tableview reloadData]; } //-(void)LoadMore //{ // //創(chuàng)建模型 // YYtgModel *tgmodel=[[YYtgModel alloc]init]; // tgmodel.title=@"菜好上桌"; // tgmodel.icon=@"5ee372ff039073317a49af5442748071"; // tgmodel.buyCount=@"20"; // tgmodel.price=@"10000"; // //將模型添加到數(shù)組中 // [self.tg addObject:tgmodel]; // // YYtgModel *tgmodelq=[[YYtgModel alloc]init]; // tgmodelq.title=@"菜好上桌1"; // tgmodelq.icon=@"5ee372ff039073317a49af5442748071"; // tgmodelq.buyCount=@"20"; // tgmodelq.price=@"10000"; // // [self.tg addObject:tgmodelq]; // //刷新表格 // [self.tableview reloadData]; //}- (void)viewDidLoad {[super viewDidLoad];self.tableview.rowHeight=80.f;//加載底部視圖//從xib中獲取數(shù)據(jù)UINib *nib=[UINib nibWithNibName:@"YYfooterview" bundle:nil];YYfooterview *footerview=[[nib instantiateWithOwner:nil options:nil] firstObject];self.tableview.tableFooterView=footerview;//設(shè)置控制 // footerview.controller=self;//設(shè)置代理footerview.delegate=self; } #pragma mark- 懶加載 -(NSArray *)tg {if (_tg==nil) {NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"tgs.plist" ofType:nil];NSArray *temparray=[NSArray arrayWithContentsOfFile:fullpath];NSMutableArray *arrayM=[NSMutableArray arrayWithCapacity:temparray.count];for (NSDictionary *dict in temparray) {YYtgModel *tg=[YYtgModel tgWithDict:dict];[arrayM addObject:tg];}_tg=arrayM;}return _tg; }#pragma mark- xib創(chuàng)建cell數(shù)據(jù)處理#pragma mark 多少組 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {return 1; }#pragma mark多少行 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {return self.tg.count; }#pragma mark設(shè)置每組每行 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {//1.創(chuàng)建cellYYtgcell *cell=[YYtgcell tgcellWithTableView:tableView];//2.獲取當(dāng)前行的模型,設(shè)置cell的數(shù)據(jù)YYtgModel *tg=self.tg[indexPath.row];cell.yytg=tg;//3.返回cellreturn cell; }#pragma mark- 隱藏狀態(tài)欄 -(BOOL)prefersStatusBarHidden {return YES; }@end?
轉(zhuǎn)載于:https://www.cnblogs.com/yipingios/p/5549874.html
總結(jié)
以上是生活随笔為你收集整理的iOS开发UI篇-在UItableview中实现加载更多功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PCIE2.0/PCIE3.0/PCIE
- 下一篇: InfiniBand简介