******* 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. css 让内容满屏居中不变形

    .selector { position: fixed; width: 100%; height: 100%; background-image: url(path); background-repe ...

  2. (转)ACM next_permutation函数

    转自 stven_king的博客 这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记  (1) int 类型的next_permuta ...

  3. SPOJ SUBST1 后缀数组

    题目链接:http://www.spoj.com/problems/SUBST1/en/ 题意:给定一个字符串,求不相同的子串个数. 思路:直接根据09年oi论文<<后缀数组——出来字符串 ...

  4. react-router

    基本的构建 import ReactRouter from 'react-router'; let {Route, Router, Link, IndexRoute} = ReactRouter.Ro ...

  5. hdu 5269 ZYB loves Xor I

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission( ...

  6. CodeForceS#276-B(求最大值)

    B. Valuable Resources   Many computer strategy games require building cities, recruiting army, conqu ...

  7. NUC_HomeWork1 -- POJ1088(DP)

    D - 滑雪 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Mic ...

  8. apache htpasswd.exe创建密码

    一.使用apache htpasswd.exe创建密码文件,命令请看PHP推荐教程:apache htpasswd命令用法详解 apache htpasswd命令用法实例 1.如何利用htpasswd ...

  9. ccc 调试方法

    当修改完一个函数,但是不知道哪个函数调用的时候没有传递正确的参数的时候 需要找出调用这个函数的所有语句,于是我注释掉这个函数就可以了

  10. BZOJ4379 : [POI2015]Modernizacja autostrady

    两遍树形DP求出每个点开始往上往下走的前3长路以及每个点上下部分的直径. 枚举每条边断开,设两边直径分别为$A,B$,则: 对于第一问,连接两边直径的中点可得直径为$\max(A,B,\lfloor\ ...