一、实现效果

点击加载更多按钮,出现一个加载图示,三秒钟后添加两条新的数据。

                    

二、实现代码和说明

当在页面(视图部分)点击加载更多按钮的时候,主页面(主控制器)会加载两条数据进来。

视图部分的按钮被点击的时候,要让主控制器加载数据,刷新表格,2B青年会在视图中增加一个主控制器的属性,通过这个属性去调用进行加载,但在开发中通常通过代理模式来完成这个操作。

下面分别是两种实现的代码。

1、项目结构和说明

说明:加载更多永远都放在这个tableview的最下端,因此这里设置成了这个tableview的tableFooterView。

self.tableview.tableFooterView=footerview;

在实现上通过xib来进行处理,考虑到左右的留白,以及点击后的要切换到加载按钮和文字,要同时控制图标和文字,因此把加载图标和文字提示放在了一个view中以便控制,这个xib已经和YYfooterview.xib进行了关联,通过这个类来控制xib。

2、实现代码

(1).垃圾代码

数据模型部分

YYtg.h文件

 1 //2 //  YYtg.h3 //  02-团购(使用xib和类完成数据展示)4 //5 //  Created by apple on 14-5-29.6 //  Copyright (c) 2014年 itcase. All rights reserved.7 //8 9 #import <Foundation/Foundation.h>
10 #import "Global.h"
11
12 @interface YYtgModel : NSObject
13 @property(nonatomic,copy)NSString *icon;
14 @property(nonatomic,copy)NSString *buyCount;
15 @property(nonatomic,copy)NSString *title;
16 @property(nonatomic,copy)NSString *price;
17
18 //对外接口
19 YYinitH(tg)
20 @end

YYtg.m文件

 1 //2 //  YYtg.m3 //  02-团购(使用xib和类完成数据展示)4 //5 //  Created by apple on 14-5-29.6 //  Copyright (c) 2014年 itcase. All rights reserved.7 //8 9 #import "YYtgModel.h"
10
11 @implementation YYtgModel
12 YYinitM(tg)
13 @end

注意:对于数据转模型部分的构造方法接口和实现代码已经通过自定义带参数的宏来进行了封装。

封装代码如下:

 1 #ifndef _0____________Global_h2 #define _0____________Global_h3 4 /**5  *  自定义带参数的宏6  */7 #define     YYinitH(name)   -(instancetype)initWithDict:(NSDictionary *)dict;\8 +(instancetype)name##WithDict:(NSDictionary *)dict;9
10
11 #define     YYinitM(name)  -(instancetype)initWithDict:(NSDictionary *)dict\
12 {\
13     if (self=[super init]) {\
14         [self setValuesForKeysWithDictionary:dict];\
15     }\
16     return self;\
17 }\
18 \
19 +(instancetype)name##WithDict:(NSDictionary *)dict\
20 {\
21     return [[self alloc]initWithDict:dict];\
22 }\
23
24 #endif

视图部分

YYtgcell.h文件

 1 //2 //  YYtgcell.h3 //  02-团购(使用xib和类完成数据展示)4 //5 //  Created by apple on 14-5-29.6 //  Copyright (c) 2014年 itcase. All rights reserved.7 //8 9 #import <UIKit/UIKit.h>
10 #import "YYtgModel.h"
11
12 @interface YYtgcell : UITableViewCell
13 @property(nonatomic,strong)YYtgModel *yytg;
14
15 //把加载数据(使用xib创建cell的内部细节进行封装)
16 +(instancetype)tgcellWithTableView:(UITableView *)tableView;
17 @end

YYtgcell.m文件

 1 //2 //  YYtgcell.m3 //  02-团购(使用xib和类完成数据展示)4 //5 //  Created by apple on 14-5-29.6 //  Copyright (c) 2014年 itcase. All rights reserved.7 //8 9 #import "YYtgcell.h"
10 //私有扩展
11 @interface YYtgcell()
12 @property (strong, nonatomic) IBOutlet UIImageView *img;
13 @property (strong, nonatomic) IBOutlet UILabel *titlelab;
14 @property (strong, nonatomic) IBOutlet UILabel *pricelab;
15 @property (strong, nonatomic) IBOutlet UILabel *buycountlab;
16 @end
17 @implementation YYtgcell
18
19 #pragma mark 重写set方法,完成数据的赋值操作
20 -(void)setYytg:(YYtgModel *)yytg
21 {
22     _yytg=yytg;
23     self.img.image=[UIImage imageNamed:yytg.icon];
24     self.titlelab.text=yytg.title;
25     self.pricelab.text=[NSString stringWithFormat:@"$%@",yytg.price];
26     self.buycountlab.text=[NSString stringWithFormat:@"已有%@人购买",yytg.buyCount];
27 }
28
29 +(instancetype)tgcellWithTableView:(UITableView *)tableView
30 {
31     static NSString *identifier= @"tg";
32     YYtgcell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
33     if (cell==nil) {
34         //如何让创建的cell加个戳
35         //对于加载的xib文件,可以到xib视图的属性选择器中进行设置
36         cell=[[[NSBundle mainBundle]loadNibNamed:@"tgcell" owner:nil options:nil]firstObject];
37         NSLog(@"创建了一个cell");
38     }
39     return cell;
40 }
41
42 @end

YYfooterview.h文件

 1 //2 //  YYfooterview.h3 //  02-团购(使用xib和类完成数据展示)4 //5 //  Created by apple on 14-5-29.6 //  Copyright (c) 2014年 itcase. All rights reserved.7 //8 9 #import <UIKit/UIKit.h>
10 @class YYViewController;
11 @interface YYfooterview : UIView
12 @property(nonatomic,strong) YYViewController *controller;
13 @end

YYfooterview.m文件

 1 //2 //  YYfooterview.m3 //  02-团购(使用xib和类完成数据展示)4 //5 //  Created by apple on 14-5-29.6 //  Copyright (c) 2014年 itcase. All rights reserved.7 //8 9 #import "YYfooterview.h"
10 #import "YYViewController.h"
11
12 @interface YYfooterview ()
13 @property (strong, nonatomic) IBOutlet UIActivityIndicatorView *loadingview;
14 @property (strong, nonatomic) IBOutlet UIButton *loadbtn;
15
16 @end
17 @implementation YYfooterview
18 - (IBAction)loadbtclick {
19     NSLog(@"按钮被点击了");
20     //隐藏按钮
21     self.loadbtn.hidden=YES;
22     //显示菊花
23     self.loadingview.hidden=NO;
24
25 #warning 模拟发送网络请求, 3秒之后隐藏菊花
26     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
27         // 3.调用控制的加载数据方法
28         [self.controller LoadMore];
29         // 4.隐藏菊花视图
30         self.loadingview.hidden = YES;
31         // 5.显示按钮
32         self.loadbtn.hidden = NO;
33     });
34 }
35
36 @end

主控制器

YYViewController.h文件

 1 //2 //  YYViewController.h3 //  02-团购(使用xib和类完成数据展示)4 //5 //  Created by apple on 14-5-29.6 //  Copyright (c) 2014年 itcase. All rights reserved.7 //8 9 #import <UIKit/UIKit.h>
10
11 @interface YYViewController : UIViewController
12 //公开接口
13 //- (void)LoadMore;
14 @end

YYViewController.m文件

  1 //2 //  YYViewController.m3 //  02-团购(使用xib和类完成数据展示)4 //5 //  Created by apple on 14-5-29.6 //  Copyright (c) 2014年 itcase. All rights reserved.7 //8 9 #import "YYViewController.h"10 #import "YYtgModel.h"11 #import "YYtgcell.h"12 #import "YYfooterview.h"13 14 @interface YYViewController ()<UITableViewDataSource,UITableViewDelegate>15 @property (strong, nonatomic) IBOutlet UITableView *tableview;16 17 @property(strong,nonatomic)NSMutableArray *tg;18 @end19 20 @implementation YYViewController21 22 #pragma mark-加载数据方法23 -(void)LoadMore24 {25     //创建模型26     YYtgModel *tgmodel=[[YYtgModel alloc]init];27     tgmodel.title=@"菜好上桌";28     tgmodel.icon=@"5ee372ff039073317a49af5442748071";29     tgmodel.buyCount=@"20";30     tgmodel.price=@"10000";31     //将模型添加到数组中32     [self.tg addObject:tgmodel];33     34     YYtgModel *tgmodelq=[[YYtgModel alloc]init];35     tgmodelq.title=@"菜好上桌1";36     tgmodelq.icon=@"5ee372ff039073317a49af5442748071";37     tgmodelq.buyCount=@"20";38     tgmodelq.price=@"10000";39     40     [self.tg addObject:tgmodelq];41     //刷新表格42     [self.tableview reloadData];43 }44 45 - (void)viewDidLoad46 {47     [super viewDidLoad];48     self.tableview.rowHeight=80.f;49     50     //加载底部视图51     //从xib中获取数据52     UINib *nib=[UINib nibWithNibName:@"YYfooterview" bundle:nil];53     YYfooterview *footerview=[[nib instantiateWithOwner:nil options:nil] firstObject];54     self.tableview.tableFooterView=footerview;55     //设置控制56     footerview.controller=self;57 }58 #pragma mark-  懒加载59 -(NSArray *)tg60 {61     if (_tg==nil) {62         NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"tgs.plist" ofType:nil];63         NSArray *temparray=[NSArray arrayWithContentsOfFile:fullpath];64         65         NSMutableArray *arrayM=[NSMutableArray arrayWithCapacity:temparray.count];66         for (NSDictionary *dict in temparray) {67             YYtgModel *tg=[YYtgModel tgWithDict:dict];68             [arrayM addObject:tg];69         }70         _tg=arrayM;71     }72     return _tg;73 }74 75 #pragma mark- xib创建cell数据处理76 77 #pragma mark 多少组78 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView79 {80     return 1;81 }82 83 #pragma mark多少行84 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section85 {86     return self.tg.count;87 }88 89 #pragma mark设置每组每行90 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath91 {92     //1.创建cell93     YYtgcell *cell=[YYtgcell tgcellWithTableView:tableView];94    95     //2.获取当前行的模型,设置cell的数据96     YYtgModel *tg=self.tg[indexPath.row];97     cell.yytg=tg;98     99     //3.返回cell
100     return cell;
101 }
102
103 #pragma mark- 隐藏状态栏
104 -(BOOL)prefersStatusBarHidden
105 {
106     return YES;
107 }
108
109 @end

2.通过代理完成

当按钮被点击的时候,视图部分本身不干活,而是通知它的代理(控制器)完成接下来的操作。

该部分代码在1的基础上对下面几个文件进行了修改:

视图部分:

YYfooterview.h文件

 1 //2 //  YYfooterview.h3 //  02-团购(使用xib和类完成数据展示)4 //5 //  Created by apple on 14-5-29.6 //  Copyright (c) 2014年 itcase. All rights reserved.7 //8 9 #import <UIKit/UIKit.h>
10 @class YYViewController ,YYfooterview;
11 //约定协议
12 @protocol YYfooterviewDelegate <NSObject>
13 -(void)footerviewLoadMore;
14 @end
15
16 @interface YYfooterview : UIView
17
18 //声明一个id类型属性,遵守了协议的“人”即可成为它的代理
19 @property(nonatomic,strong)id<YYfooterviewDelegate> delegate;
20 //@property(nonatomic,strong) YYViewController *controller;
21 @end

YYfooterview.m文件

 1 //2 //  YYfooterview.m3 //  02-团购(使用xib和类完成数据展示)4 //5 //  Created by apple on 14-5-29.6 //  Copyright (c) 2014年 itcase. All rights reserved.7 //8 9 #import "YYfooterview.h"
10 #import "YYViewController.h"
11
12 @interface YYfooterview ()
13 @property (strong, nonatomic) IBOutlet UIActivityIndicatorView *loadingview;
14 @property (strong, nonatomic) IBOutlet UIButton *loadbtn;
15
16 @end
17 @implementation YYfooterview
18 - (IBAction)loadbtclick {
19     NSLog(@"按钮被点击了");
20     //隐藏按钮
21     self.loadbtn.hidden=YES;
22     //显示菊花
23     self.loadingview.hidden=NO;
24
25 #warning 模拟发送网络请求, 3秒之后隐藏菊花
26     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
27         // 3.调用控制的加载数据方法
28 //        [self.controller LoadMore];
29         //通知代理
30         [self.delegate footerviewLoadMore];
31         // 4.隐藏菊花视图
32         self.loadingview.hidden = YES;
33         // 5.显示按钮
34         self.loadbtn.hidden = NO;
35     });
36 }
37
38 @end

主控制器部分

YYViewController.h文件

 1 //2 //  YYViewController.h3 //  02-团购(使用xib和类完成数据展示)4 //5 //  Created by apple on 14-5-29.6 //  Copyright (c) 2014年 itcase. All rights reserved.7 //8 9 #import <UIKit/UIKit.h>
10
11 @interface YYViewController : UIViewController
12 //公开接口
13 //- (void)LoadMore;
14 @end

YYViewController.m文件

  1 //2 //  YYViewController.m3 //  02-团购(使用xib和类完成数据展示)4 //5 //  Created by apple on 14-5-29.6 //  Copyright (c) 2014年 itcase. All rights reserved.7 //8 9 #import "YYViewController.h"10 #import "YYtgModel.h"11 #import "YYtgcell.h"12 #import "YYfooterview.h"13 14 @interface YYViewController ()<UITableViewDataSource,UITableViewDelegate,YYfooterviewDelegate>15 @property (strong, nonatomic) IBOutlet UITableView *tableview;16 17 @property(strong,nonatomic)NSMutableArray *tg;18 @end19 20 @implementation YYViewController21 22 #pragma mark-加载数据方法23 -(void)footerviewLoadMore24 {25     //创建模型26     YYtgModel *tgmodel=[[YYtgModel alloc]init];27     tgmodel.title=@"菜好上桌";28     tgmodel.icon=@"5ee372ff039073317a49af5442748071";29     tgmodel.buyCount=@"20";30     tgmodel.price=@"10000";31     //将模型添加到数组中32     [self.tg addObject:tgmodel];33     34     YYtgModel *tgmodelq=[[YYtgModel alloc]init];35     tgmodelq.title=@"菜好上桌1";36     tgmodelq.icon=@"5ee372ff039073317a49af5442748071";37     tgmodelq.buyCount=@"20";38     tgmodelq.price=@"10000";39     40     [self.tg addObject:tgmodelq];41     //刷新表格42     [self.tableview reloadData];43 }44 //-(void)LoadMore45 //{46 //    //创建模型47 //    YYtgModel *tgmodel=[[YYtgModel alloc]init];48 //    tgmodel.title=@"菜好上桌";49 //    tgmodel.icon=@"5ee372ff039073317a49af5442748071";50 //    tgmodel.buyCount=@"20";51 //    tgmodel.price=@"10000";52 //    //将模型添加到数组中53 //    [self.tg addObject:tgmodel];54 //    55 //    YYtgModel *tgmodelq=[[YYtgModel alloc]init];56 //    tgmodelq.title=@"菜好上桌1";57 //    tgmodelq.icon=@"5ee372ff039073317a49af5442748071";58 //    tgmodelq.buyCount=@"20";59 //    tgmodelq.price=@"10000";60 //    61 //    [self.tg addObject:tgmodelq];62 //    //刷新表格63 //    [self.tableview reloadData];64 //}65 66 - (void)viewDidLoad67 {68     [super viewDidLoad];69     self.tableview.rowHeight=80.f;70     71     //加载底部视图72     //从xib中获取数据73     UINib *nib=[UINib nibWithNibName:@"YYfooterview" bundle:nil];74     YYfooterview *footerview=[[nib instantiateWithOwner:nil options:nil] firstObject];75     self.tableview.tableFooterView=footerview;76     //设置控制77 //    footerview.controller=self;78     //设置代理79     footerview.delegate=self;80 }81 #pragma mark-  懒加载82 -(NSArray *)tg83 {84     if (_tg==nil) {85         NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"tgs.plist" ofType:nil];86         NSArray *temparray=[NSArray arrayWithContentsOfFile:fullpath];87         88         NSMutableArray *arrayM=[NSMutableArray arrayWithCapacity:temparray.count];89         for (NSDictionary *dict in temparray) {90             YYtgModel *tg=[YYtgModel tgWithDict:dict];91             [arrayM addObject:tg];92         }93         _tg=arrayM;94     }95     return _tg;96 }97 98 #pragma mark- xib创建cell数据处理99
100 #pragma mark 多少组
101 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
102 {
103     return 1;
104 }
105
106 #pragma mark多少行
107 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
108 {
109     return self.tg.count;
110 }
111
112 #pragma mark设置每组每行
113 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
114 {
115     //1.创建cell
116     YYtgcell *cell=[YYtgcell tgcellWithTableView:tableView];
117
118     //2.获取当前行的模型,设置cell的数据
119     YYtgModel *tg=self.tg[indexPath.row];
120     cell.yytg=tg;
121
122     //3.返回cell
123     return cell;
124 }
125
126 #pragma mark- 隐藏状态栏
127 -(BOOL)prefersStatusBarHidden
128 {
129     return YES;
130 }
131
132 @end

转载于:https://www.cnblogs.com/sunflower-lhb/p/4900877.html

OS开发UI篇—在UItableview中实现加载更多功能相关推荐

  1. iOS开发UI篇-在UItableview中实现加载更多功能

    iOS开发UI篇-在UItableview中实现加载更多功能 一.实现效果 点击加载更多按钮,出现一个加载图示,三秒钟后添加两条新的数据. 二.实现代码和说明 当在页面(视图部分)点击加载更多按钮的时 ...

  2. js页面中实现加载更多功能

    js页面中实现加载更多功能 分页-如何实现加载更多功能,目前的在很多网站上使用的加载更多功能中,使用最多的是iscroll.js实现的上拉加载更多.下拉刷新功能.但是iscroll.js本身并没有集成 ...

  3. iOS开发UI篇—实现UITableview控件数据刷新

    iOS开发UI篇-实现UITableview控件数据刷新 一.项目文件结构和plist文件 二.实现效果 1.说明:这是一个英雄展示界面,点击选中行,可以修改改行英雄的名称(完成数据刷新的操作). 运 ...

  4. iOS开发UI篇—使用UItableview完成一个简单的QQ好友列表(一)

    iOS开发UI篇-使用UItableview完成一个简单的QQ好友列表(一) 一.项目结构和plist文件 二.实现代码 1.说明: 主控制器直接继承UITableViewController // ...

  5. android中如何实现上拉加载更多功能(建议收藏)

    上拉加载更多功能实际上就是给RecyclerView增加一个FooterView,然后通过判断是否滑动到了最后一条Item,来控制FooterView的显示和隐藏,接下来我们来看下如何实现: 一.给A ...

  6. OS开发UI篇—popoverController使用注意

    iOS开发UI篇-popoverController使用注意 一.设置尺寸 提示:不建议,像下面这样吧popover的宽度和高度写死. 1 //1.新建一个内容控制器2 YYMenuViewContr ...

  7. OS开发UI篇—核心动画(基础动画)

    文顶顶 最新博文发布地址:花田半亩(wendingding.com) iOS开发UI篇-核心动画(基础动画) iOS开发UI篇-核心动画(基础动画) 一.简单介绍 CAPropertyAnimatio ...

  8. 微信小程序实战篇-下拉刷新与加载更多

    下拉刷新 实现下拉刷新目前能想到的有两种方式 调用系统的API,系统有提供下拉刷新的API接口 下拉刷新API.png 监听scroll-view,自定义下拉刷新,还记得scroll-view里面有一 ...

  9. MVC中实现 加载更多...

    需要实现的功能: 数据太多想初次加载部分数据,在底部加上"加载更多"按钮 点击后加载第二页数据(从数据库只取指定页数据)后接在已有数据后面(类似于android中的下拉加载更多) ...

最新文章

  1. 2.本征矩阵 基本矩阵以及对极几何之间的约束关系
  2. SVN学习总结(1)——SVN简介及入门使用
  3. 带你封装一个上传图片组件(ant design+react)
  4. 动画编辑器和骨骼动画使用
  5. python有什么用-python有什么用(用处和用途)
  6. html5支付认证,html5实现微信支付宝接口
  7. golang json 转 map 切片 结构体
  8. (转)黑手安全网QQ工具箱第三版 黑手一周年纪念版!
  9. 黑苹果睡眠重启后死机_iPhone 所有机型“死机/卡机”重启方法
  10. thinkphp使用163/126邮箱发送
  11. 麦客服务器维修,麦客
  12. font: 12px/1.5 Tahoma, Helvetica, Arial, sans-serif;网页设计中的默认字体
  13. Linux篇-The slave I/O thread stops because master and slave have equal...
  14. 计算机原理探究第一部分教案,高中信息技术《计算机结构原理初步》教案
  15. BatchNormalization 介绍
  16. 广东移动 - 无线应用加速业务系统
  17. Rainbow 开发 step1
  18. python双星号什么运算_Python的星号(*)和双星号(**)用法
  19. Shopee大促爆单?从这些营销工具切入,订单一爆再爆
  20. CSS(二):常用CSS样式[文字、文本、背景......]

热门文章

  1. 简单的VC 操作XML 文件的的方法
  2. 乒乓球比赛赛程_丁宁休战,刘诗雯做手术!李隼、秦志戬做介绍,国乒最新赛程曝光...
  3. Typora markdown公式换行等号对齐_【精品软件 第3期】 有颜有料的编辑器——Typora
  4. mysql自增长2个增加_mysql – 添加第二个自动增量字段并允许重复
  5. java hdfs 新建目录_如何用java在hdfs中创建一个新目录?
  6. oracle 关联出现重复数据,ORACLE 分页查询出现重复记录的解决办法
  7. vue 富文本存储_Vue富文本编辑器
  8. C#中统计程序运行时间
  9. linux 获取 基地址,linux - 每个函数加载的glibc基地址不同。 - SO中文参考 - www.soinside.com...
  10. springmvc框架介绍_Java修行第071天 ---SpringMVC(上)