一、Model

 #import <Foundation/Foundation.h>

 @interface Goods : NSObject

 @property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *price;
@property (nonatomic, copy) NSString *buyCount; - (instancetype) initWithDict:(NSDictionary *)dict;
+ (instancetype) goodsWithDict:(NSDictionary *)dict; @end #import "Goods.h" @implementation Goods - (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
} + (instancetype)goodsWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
} @end

二、View

 #import <UIKit/UIKit.h>

 @interface BWHeaderView : UIView

 + (instancetype)headerView;

 @end

 #import "BWHeaderView.h"

 @interface BWHeaderView ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView; @end @implementation BWHeaderView //当这个方法被执行的时候就表示BWHeaderView已经从xib文件中创建好了
//BWHeaderView的子控件也都创建好了,所以就可以使用UIScrollView了。
- (void)awakeFromNib
{ } //创建headerView
+ (instancetype)headerView
{
BWHeaderView *headerView = [[[NSBundle mainBundle] loadNibNamed:@"BWHeaderView" owner:nil options:nil] lastObject];
return headerView;
} @end

 #import <UIKit/UIKit.h>

 // 协议命名规范:
// 类名 + Delegate
// 协议中的方法最好加@optional
// 定义一个delegate属性,delegate属性用weak
// delegate属性声明为id类型,可以用来解除对头文件的依赖 @class BWFooterView;
@protocol BWFooterViewDelegate <NSObject> @required
- (void)footerViewUpDateData:(BWFooterView *)footerView;
@end @interface BWFooterView : UIView @property(weak, nonatomic) id<BWFooterViewDelegate> delegate; + (instancetype)footerView; @end #import "BWFooterView.h" @interface BWFooterView ()
@property (weak, nonatomic) IBOutlet UIButton *btnLoadMore;
@property (weak, nonatomic) IBOutlet UIView *waittingView;
- (IBAction)btnLoadMoreClick:(id)sender; @end @implementation BWFooterView //通过xib设置tableView中的tableFooterView
+ (instancetype)footerView
{
BWFooterView *footerView = [[[NSBundle mainBundle] loadNibNamed:@"BWFooterView" owner:nil options:nil] lastObject];
return footerView;
} //加载按钮单击事件
- (IBAction)btnLoadMoreClick:(id)sender { //1.隐藏“加载更多”按钮
self.btnLoadMore.hidden = YES;
//2.显示“等待指示器”所在的那个View
self.waittingView.hidden = NO; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//调用代理方法
if ([self.delegate respondsToSelector:@selector(footerViewUpDateData:)]) {
//3.增加一条数据
//3.1创建一个模型对象
//3.2把模型对象加载控制器的goods集合中
//4.刷新UITableView
[self.delegate footerViewUpDateData:self];
}
//5.显示“加载更多”按钮
self.btnLoadMore.hidden = NO;
//6.隐藏“等待指示器”所在的那个View
self.waittingView.hidden = YES;
}); }
@end

 #import <UIKit/UIKit.h>
@class Goods; @interface BWGoodsCell : UITableViewCell @property (weak, nonatomic) IBOutlet UIImageView *imgView;
@property (weak, nonatomic) IBOutlet UILabel *lblName;
@property (weak, nonatomic) IBOutlet UILabel *lblPrice;
@property (weak, nonatomic) IBOutlet UILabel *lblBuyCount; @property (strong, nonatomic) Goods *myGoods; + (instancetype)goodsCellWithTableView:(UITableView *)tableView;
@end #import "BWGoodsCell.h"
#import "Goods.h" @interface BWGoodsCell () @end @implementation BWGoodsCell // 重写setMyGoods方法,把模型的数据设置给子控件
- (void)setMyGoods:(Goods *)myGoods
{
_myGoods = myGoods; self.imgView.image = [UIImage imageNamed:_myGoods.icon];
self.lblName.text = _myGoods.title;
self.lblPrice.text = [NSString stringWithFormat:@"$ %@",_myGoods.price];
self.lblBuyCount.text = [NSString stringWithFormat:@"%@个人已购买",_myGoods.buyCount];
} // 创建单元格
+ (instancetype)goodsCellWithTableView:(UITableView *)tableView
{
static NSString *cellIndentifier = @"cellIndentifier";
BWGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier]; if (!cell) {
cell = [[[NSBundle mainBundle] loadNibNamed:@"BWGoodsCell" owner:nil options:nil] firstObject];
}
return cell;
}
- (void)awakeFromNib {
// Initialization code
} - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated]; // Configure the view for the selected state
} @end

三、Controller

 #import "ViewController.h"
#import "Goods.h"
#import "BWGoodsCell.h"
#import "BWFooterView.h"
#import "BWHeaderView.h" @interface ViewController ()<UITableViewDataSource,BWFooterViewDelegate> @property (nonatomic, strong) NSMutableArray *arrayModel;
@property (nonatomic, strong) UITableView *tableView; @end @implementation ViewController #pragma mark - 懒加载
- (NSArray *)arrayModel
{
if (_arrayModel == nil) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"tgs.plist" ofType:nil]; NSArray *arrayDict = [NSArray arrayWithContentsOfFile:path]; NSMutableArray *arrayModel = [NSMutableArray array]; for (NSDictionary *dict in arrayDict) {
Goods *goodsModel = [Goods goodsWithDict:dict];
[arrayModel addObject:goodsModel];
}
_arrayModel = arrayModel;
} return _arrayModel;
} #pragma mark - 加载视图
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
self.tableView.dataSource =self; [self.view addSubview:self.tableView];
self.tableView.rowHeight = ; //ps:tableView 的tableFooterView特点:只能修改x和height值,y和height不能修改 //创建tableFooterView
BWFooterView *footerView = [BWFooterView footerView];
//设置footerView的代理
footerView.delegate =self;
self.tableView.tableFooterView = footerView; //创建tableHeaderView
BWHeaderView *headerView = [BWHeaderView headerView]; self.tableView.tableHeaderView = headerView; }
#pragma mark - CZFooterView的代理方法
- (void)footerViewUpDateData:(BWFooterView *)footerView
{
//3.增加一条数据
//3.1创建一个模型对象
Goods *model = [[Goods alloc] init];
model.title = @"驴肉火烧";
model.price = @"6.0";
model.buyCount = @"";
model.icon = @"7003217f16ed29bab85e635a3bd6b60d";
//3.2把模型对象加载控制器的goods集合中
[self.arrayModel addObject:model];
//4.刷新UITableView
[self.tableView reloadData]; //ps:局部刷新(仅适用于UITableView的总行数没有发生变化的时候)
// NSIndexPath *indexpath = [NSIndexPath indexPathForRow:self.arrayModel.count-1 inSection:0];
// [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationLeft]; //5.把UITableView中最后一行滚动到最上面
NSIndexPath *indexpath = [NSIndexPath indexPathForRow:self.arrayModel.count- inSection:];
[self.tableView scrollToRowAtIndexPath:indexpath atScrollPosition:UITableViewScrollPositionTop animated:YES];
} #pragma mark - 数据源
//加载组的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.arrayModel.count;
}
//加载单元格数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//1.获取数据模型
Goods *goodsModel = self.arrayModel[indexPath.row]; //2.创建单元格
BWGoodsCell *cell = [BWGoodsCell goodsCellWithTableView:tableView]; // 在控制器中直接为cell的每个子控件赋值数据造成问题
// 1>控制器强依赖于cell,一旦cell内部的子控件发生变化,那么子控件中的代码也得改(紧耦合)
// 2>cell封装不够完整,凡是用到cell的地方
// 3>解决:直接把模型传递给自定义cell,然后在自定义cell内部解析model中的数据赋值给自定义cell的内部的子控件 //3.把模型数据设置给单元格
cell.myGoods = goodsModel; //4.返回单元格
return cell;
} #pragma mark - 状态栏
- (BOOL)prefersStatusBarHidden
{
return YES;
} #pragma mark - 内存
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

最新文章

  1. 单调队列 &amp;&amp; 斜率优化dp 专题
  2. NodeMCU初探
  3. bootstrap实现嵌入的button
  4. CSS对字体单位的总结
  5. 抛砖引玉:关于Android的ListView中CheckBox错乱
  6. Shiro 缓存失效以后的一个问题
  7. poj2007
  8. 【转】Apache配置中ProxyPassReverse指令的含义
  9. oracle数据库的数据类型
  10. struts配置文件和国际化
  11. Robocopy 轉帖
  12. 通过添加filter过滤器 彻底解决ajax 跨域问题
  13. 视频流GPU解码在ffempg的实现(二)-GPU解码器
  14. 微信公众号的开发 Senparc.Weixin.dll使用
  15. 部署vmware-vcsa 6.5
  16. Unity应用架构设计(6)——设计动态数据集合ObservableList
  17. CentOS7 linux下yum安装redis以及使用
  18. (转)HIBERNATE与 MYBATIS的对比
  19. GBDT算法
  20. 2019.1.7 Russia temperature control demo

热门文章

  1. Python入门之Python Colorama模块
  2. Python3基础 os chdir 改变工作目录
  3. BZOJ 3339 &amp;&amp; luogu4137 Rmq Problem / mex(莫队)
  4. redhat7 防火墙设置
  5. ACM-ICPC 2018 南京赛区网络预赛 E AC Challenge 状压DP
  6. orm框架综合
  7. DPDK的安装与绑定网卡
  8. js中一些关于比较左右两边的值的题目
  9. LA 4080 战争和物流(最短路树)
  10. Centos下挖XMR门罗币的详细教程