******* HMViewController.h

#import "HMViewController.h"
#import "HMTg.h"
#import "HMTgCell.h"
#import "HMTgFooterView.h" @interface HMViewController () <HMTgFooterViewDelegate>
@property (nonatomic, strong) NSMutableArray *tgs;
@end @implementation HMViewController - (NSArray *)tgs
{
if (_tgs == nil) _tgs = [HMTg tgs]; //数据转模型
return _tgs;
} - (void)viewDidLoad
{
[super viewDidLoad]; self.tableView.rowHeight = ; // 调整边距,可以让表格视图让开状态栏
self.tableView.contentInset = UIEdgeInsetsMake(, , , ); // footerView
// footerView的宽度会和表格整体宽度一致,只需要指定高度即可
// self.tableView.tableFooterView = [UIButton buttonWithType:UIButtonTypeContactAdd];
// UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 44)];
// view.backgroundColor = [UIColor redColor];
// self.tableView.tableFooterView = view;
// 从XIB加载最后一个视图设置为footerView
HMTgFooterView *footer = [HMTgFooterView footerView];
// 视图控制器成为footerView的代理
footer.delegate = self;
self.tableView.tableFooterView = footer; self.tableView.tableHeaderView = [[[NSBundle mainBundle] loadNibNamed:@"HMTgHeadView" owner:nil options:nil] lastObject];
} ///** 隐藏状态栏 */
//- (BOOL)prefersStatusBarHidden
//{
// return YES;
//} #pragma mark - footerView的代理方法
/**
预处理指令
#if 0
所有代码都不会执行 #endif
*/
#if 1
- (void)tgFooterViewDidDownloadButtonClick:(HMTgFooterView *)footerView //代理来的方法
{
// 加载数据
NSLog(@"努力加载数据中...."); // 向数组中添加数据,模拟网络加载完成之后的效果
NSDictionary *dict = @{@"title": @"哈哈", @"icon": @"ad_00", @"price": @"100.2", @"buyCount": @""};
HMTg *tg = [HMTg tgWithDict:dict]; NSLog(@"加数据前 %d", self.tgs.count); [self.tgs addObject:tg]; NSLog(@"加数据后 %d", self.tgs.count);
// 刷新数据
// [self.tableView reloadData];
// 新建一个indexPath
NSIndexPath *path = [NSIndexPath indexPathForRow:(self.tgs.count - ) inSection:];
[self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationMiddle];
}
#endif #pragma mark - 数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.tgs.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1. 创建cell
HMTgCell *cell = [HMTgCell cellWithTableView:tableView]; // 2. 通过数据模型,设置Cell内容,可以让视图控制器不需要了解cell内部的实现细节
cell.tg = self.tgs[indexPath.row]; return cell;
} @end

***HMTgCell.m

#import "HMTgCell.h"
#import "HMTg.h" @interface HMTgCell()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *buyCountLabel;
@end @implementation HMTgCell + (instancetype)cellWithTableView:(UITableView *)tableView
{
// 1. 可重用标示符
static NSString *ID = @"Cell";
// 2. tableView查询可重用Cell
HMTgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 3. 如果没有可重用cell
if (cell == nil) {
NSLog(@"加载XIB");
// 从XIB加载自定义视图
cell = [[[NSBundle mainBundle] loadNibNamed:@"HMTgCell" owner:nil options:nil] lastObject];
} return cell;
} - (void)setTg:(HMTg *)tg
{
// setter方法中,第一句要赋值,否则要在其他方法中使用模型,将无法访问到
_tg = tg; self.titleLabel.text = tg.title;
self.iconView.image = [UIImage imageNamed:tg.icon];
self.priceLabel.text = tg.price;
self.buyCountLabel.text = tg.buyCount;
} #pragma mark - 模板提供的方法
/**
初始化方法 使用代码创建Cell的时候会被调用,如果使用XIB或者Storyboard,此方法不会被调用
*/
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
NSLog(@"%s", __func__);
}
return self;
} /**
从XIB被加载之后,会自动被调用,如果使用纯代码,不会被执行
*/
- (void)awakeFromNib
{
NSLog(@"%s", __func__);
self.contentView.backgroundColor = [UIColor clearColor];
} /**
Cell 被选中或者取消选中是都会被调用 如果是自定义Cell控件,所有的子控件都应该添加到contentView中
*/
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated]; if (selected) {
self.contentView.backgroundColor = [UIColor redColor];
} else {
self.contentView.backgroundColor = [UIColor greenColor];
}
} @end

*********HMTgCell.h

#import <UIKit/UIKit.h>
@class HMTg; @interface HMTgCell : UITableViewCell
/** 团购的数据模型 */
@property (nonatomic, strong) HMTg *tg; /** 提供一个类方法,可以快速创建 Cell */
+ (instancetype)cellWithTableView:(UITableView *)tableView; @end

HMTgFooterView.h

#import <UIKit/UIKit.h>
@class HMTgFooterView; @protocol HMTgFooterViewDelegate <NSObject> @optional
/** 视图的下载按钮被点击 */
- (void)tgFooterViewDidDownloadButtonClick:(HMTgFooterView *)footerView; @end @interface HMTgFooterView : UIView // 代理如果使用强引用,就会产生循环引用,造成控制器和子视图都无法被释放,造成内存泄露
@property (nonatomic, weak) id <HMTgFooterViewDelegate> delegate; + (instancetype)footerView; @end

HMTgFooterView.m

#import "HMTgFooterView.h"

@interface HMTgFooterView()
@property (weak, nonatomic) IBOutlet UIButton *loadMoreButton;
@property (weak, nonatomic) IBOutlet UIView *tipsView;
@end @implementation HMTgFooterView + (instancetype)footerView
{
return [[[NSBundle mainBundle] loadNibNamed:@"HMTgFooterView" owner:nil options:nil] lastObject];
} - (IBAction)loadMore
{
NSLog(@"加载更多");
// 1. 隐藏按钮
self.loadMoreButton.hidden = YES;
// 2. 显示提示视图
self.tipsView.hidden = NO; // 延时执行命令,多线程GCD
// 今后关于延时的操作,统一使用dispatch_after
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 块代码中的代码会在1.0秒之后执行 // 3. 加载数据(从网络服务器加载,需要时间...)
// view是用来显示数据的,用代理来实现!
// 代理是用来监听的,发生某些事情的时候,通知"代理"-》控制器去"工作"->加载数据 // 3.1 判断代理是否实现了协议方法
if ([self.delegate respondsToSelector:@selector(tgFooterViewDidDownloadButtonClick:)]) {
[self.delegate tgFooterViewDidDownloadButtonClick:self];
} // 4. 加载完成数据
self.loadMoreButton.hidden = NO;
self.tipsView.hidden = YES;
});
} @end

IOS第八天(2:UITableViewController团购,点击底部,xib加载更多, 代理模式)的更多相关文章

  1. IOS第八天(3:UITableViewController团购, 点击底部代码调整)

    ****代理者的方法中 // 通知页脚视图调整视图显示状态 [footerView endRefresh]; //发送代理通知的类中 /** 视图控制器刷新完成调用方法 */ - (void)endR ...

  2. IOS第八天(1:UITableViewController团购,数据转模型,xib显示数据)

    ******HMTg.h 模型数据 #import <Foundation/Foundation.h> @interface HMTg : NSObject @property (nona ...

  3. iOS开发 XML解析和下拉刷新,上拉加载更多

    iOS开发 XML解析和下拉刷新,上拉加载更多 1.XML格式 <?xml version="1.0" encoding="utf-8" ?> 表示 ...

  4. IOS 开发下拉刷新和上拉加载更多

    IOS 开发下拉刷新和上拉加载更多 简介 1.常用的下拉刷新的实现方式 (1)UIRefreshControl (2)EGOTTableViewrefresh (3)AH3DPullRefresh ( ...

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

    一.实现效果 点击加载更多按钮,出现一个加载图示,三秒钟后添加两条新的数据.                      二.实现代码和说明 当在页面(视图部分)点击加载更多按钮的时候,主页面(主控制器 ...

  6. ecshop 团购点击价格变动

    前提:价格阶梯只能设置一级 需要用到: jquery,transport.js(transport_jquery.js),Ajax.call html页面 js代码,还需要插入jquery,trans ...

  7. ios中自定义tableView,CollectionView的cell什么时候用nib加载,什么时候用标识重用

    做了一段时间的iOS,在菜鸟的路上还有很长的路要走,把遇到的问题记下来,好记性不如烂笔头. 在项目开发中大家经常会用到tableView和collectionView两个控件,然而在cell的自定义上 ...

  8. iOS 设计 用户为王 - 关于征询授权、注册及加载等待的体验优化

    你要做的东西一定要是你无比渴望这世界上能出现的东西,这股热情和能量将会融入到你的应用中,成为它腾飞的初速度,为你带来积极反馈.把自己当做app最重要的用户,这一点非常重要. http://www.co ...

  9. IOS第八天(7:UITableViewController新浪微博,cell 复用的简单写法优化和cell高度从模型中获取)

    *********** #import "HMViewController.h" #import "HMStatus.h" #import "HMSt ...

随机推荐

  1. (转)set集合的用法

    原地址:http://blog.csdn.net/chaiwenjun000/article/details/50561775 SET 集合 百度百科中说集合中的元素有三个特征: 1.确定性(集合中的 ...

  2. 使用frameset时的target属性

    http://blog.sina.com.cn/s/blog_8f82e8280101bwx9.html

  3. 线段树(区间合并) LA 3989 "Ray, Pass me the dishes!"

    题目传送门 题意:动态最大连续子序列和,静态的题目 分析:nlogn的归并思想.线段树维护结点的三个信息,最大前缀和,最大后缀和,该区间的最大和的两个端点,然后答案是三个的better.书上用pair ...

  4. shell实现https登录

    服务端提供了两个api: 一个是用于用户认证,因为要传输密钥,所以用了https方式  如何在服务端配置https请见另外一个博文 https://192.168.1.190:8443/api/aut ...

  5. Codeforces 627A XOR Equation(思路)

    题目大概说两个正整数a.b,已知s=a+b以及x=a xor b的值,问有几种a.b这样的数对. 我知道异或相当于无进位的加法,s-x就是其各个位置的进位,比如s-x=1010,那就表示a和b的第1位 ...

  6. EF框架step by step(1)—Database-First

    ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案,现已经包含在 Visual Studio 2008 S ...

  7. oracle查看表实际物理空间使用大小

    http://www.2cto.com/database/201107/95313.html 一种是分配给一个表的物理空间数量,而不管空间是否被使用.可以这样查询获得字节数:select segmen ...

  8. Extjs 属性控件[转载]

    Ext.form.TimeField: 配置项:            maxValue:列表中允许的最大时间            maxText:当时间大于最大值时的错误提示信息          ...

  9. PE的一些水 3-50

    T3: 分解质因数. lalala T4: 暴模. 然而数学方法怎么搞?---->也就是怎么手算?... 于是看了一下讨论区...发现原来我的数学已经低于小学生水平了... 我们把答案abccb ...

  10. UVA 11754 (暴力+中国剩余定理)

    题目链接: http://www.bnuoj.com/v3/problem_show.php?pid=20172 题目大意:有C个模方程,每个方程可能有k余数,求最小的S个解. 解题思路: 看见模方程 ...