UI029---UITableView中加载数据

一、实现效果
这里写图片描述

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

这里写图片描述

二、实现代码和说明

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

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

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

1、项目结构和说明

这里写图片描述

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

self.tableview.tableFooterView=footerview;

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

2、实现代码

.通过代理完成

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

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

视图部分:

YYfooterview.h文件


 1 //
 2 //  YYfooterview.h
 3 //  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.m
 3 //  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.h
 3 //  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.m
  3 //  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 @end
 19 
 20 @implementation YYViewController
 21 
 22 #pragma mark-加载数据方法
 23 -(void)footerviewLoadMore
 24 {
 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)LoadMore
 45 //{
 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)viewDidLoad
 67 {
 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 *)tg
 83 {
 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在UITableView的section添加数据,你需要先创建一个包含所需数据的数组。然后,在UITableViewDataSource协议实现以下方法: 1. numberOfSections(in tableView: UITableView) -> Int:返回表格的section数。 2. tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int:返回指定section的行数。 3. tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell:返回指定indexPath的UITableViewCell实例。 例如,假设你有一个包含多个section的UITableView,每个section都包含一个字符串数组。以下是一个示例代码: ``` class ViewController: UIViewController, UITableViewDataSource { var data: [[String]] = [["item 1", "item 2"], ["item 3", "item 4", "item 5"]] @IBOutlet weak var tableView: UITableView! override func viewDidLoad() { super.viewDidLoad() tableView.dataSource = self } // MARK: - UITableViewDataSource func numberOfSections(in tableView: UITableView) -> Int { return data.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return data[section].count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) cell.textLabel?.text = data[indexPath.section][indexPath.row] return cell } } ``` 在这个例子,我们创建了一个包含两个section的UITableView。每个section都有一个字符串数组,我们将其存储在data数组。在numberOfSections方法,我们返回data数组的数量,即section的数量。在tableView(_:numberOfRowsInSection:)方法,我们返回特定section的行数。最后,在tableView(_:cellForRowAt:)方法,我们获取特定indexPath的字符串并将其显示在UITableViewCell。 注意,在上述示例代码,我们将UITableViewCell标识符设置为“Cell”,你需要确保在Storyboard或xib文件对应的UITableViewCell的标识符也设置为“Cell”。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值