一、Model

 1 #import <Foundation/Foundation.h>
 2
 3 @interface Goods : NSObject
 4
 5 @property (nonatomic, copy) NSString *icon;
 6 @property (nonatomic, copy) NSString *title;
 7 @property (nonatomic, copy) NSString *price;
 8 @property (nonatomic, copy) NSString *buyCount;
 9
10 - (instancetype) initWithDict:(NSDictionary *)dict;
11 + (instancetype) goodsWithDict:(NSDictionary *)dict;
12
13
14 @end
15
16 #import "Goods.h"
17
18 @implementation Goods
19
20 - (instancetype)initWithDict:(NSDictionary *)dict
21 {
22     if (self = [super init]) {
23         [self setValuesForKeysWithDictionary:dict];
24     }
25     return self;
26 }
27
28 + (instancetype)goodsWithDict:(NSDictionary *)dict
29 {
30     return [[self alloc] initWithDict:dict];
31 }
32
33 @end

二、View

 1 #import <UIKit/UIKit.h>
 2
 3 @interface BWHeaderView : UIView
 4
 5 + (instancetype)headerView;
 6
 7 @end
 8
 9 #import "BWHeaderView.h"
10
11 @interface BWHeaderView ()
12 @property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
13
14 @end
15
16 @implementation BWHeaderView
17
18 //当这个方法被执行的时候就表示BWHeaderView已经从xib文件中创建好了
19 //BWHeaderView的子控件也都创建好了,所以就可以使用UIScrollView了。
20 - (void)awakeFromNib
21 {
22
23 }
24
25 //创建headerView
26 + (instancetype)headerView
27 {
28     BWHeaderView *headerView = [[[NSBundle mainBundle] loadNibNamed:@"BWHeaderView" owner:nil options:nil] lastObject];
29     return headerView;
30 }
31
32 @end

 1 #import <UIKit/UIKit.h>
 2
 3 // 协议命名规范:
 4 // 类名 + Delegate
 5 // 协议中的方法最好加@optional
 6 // 定义一个delegate属性,delegate属性用weak
 7 // delegate属性声明为id类型,可以用来解除对头文件的依赖
 8
 9 @class BWFooterView;
10 @protocol  BWFooterViewDelegate <NSObject>
11
12 @required
13 - (void)footerViewUpDateData:(BWFooterView *)footerView;
14 @end
15
16 @interface BWFooterView : UIView
17
18 @property(weak, nonatomic) id<BWFooterViewDelegate> delegate;
19
20 + (instancetype)footerView;
21
22 @end
23
24 #import "BWFooterView.h"
25
26 @interface BWFooterView ()
27 @property (weak, nonatomic) IBOutlet UIButton *btnLoadMore;
28 @property (weak, nonatomic) IBOutlet UIView *waittingView;
29 - (IBAction)btnLoadMoreClick:(id)sender;
30
31 @end
32
33 @implementation BWFooterView
34
35 //通过xib设置tableView中的tableFooterView
36 + (instancetype)footerView
37 {
38     BWFooterView *footerView = [[[NSBundle mainBundle] loadNibNamed:@"BWFooterView" owner:nil options:nil] lastObject];
39     return footerView;
40 }
41
42 //加载按钮单击事件
43 - (IBAction)btnLoadMoreClick:(id)sender {
44
45     //1.隐藏“加载更多”按钮
46     self.btnLoadMore.hidden = YES;
47     //2.显示“等待指示器”所在的那个View
48     self.waittingView.hidden = NO;
49
50     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
51         //调用代理方法
52         if ([self.delegate respondsToSelector:@selector(footerViewUpDateData:)]) {
53             //3.增加一条数据
54             //3.1创建一个模型对象
55             //3.2把模型对象加载控制器的goods集合中
56             //4.刷新UITableView
57             [self.delegate footerViewUpDateData:self];
58         }
59         //5.显示“加载更多”按钮
60         self.btnLoadMore.hidden = NO;
61         //6.隐藏“等待指示器”所在的那个View
62         self.waittingView.hidden = YES;
63     });
64
65 }
66 @end

 1 #import <UIKit/UIKit.h>
 2 @class Goods;
 3
 4 @interface BWGoodsCell : UITableViewCell
 5
 6 @property (weak, nonatomic) IBOutlet UIImageView *imgView;
 7 @property (weak, nonatomic) IBOutlet UILabel *lblName;
 8 @property (weak, nonatomic) IBOutlet UILabel *lblPrice;
 9 @property (weak, nonatomic) IBOutlet UILabel *lblBuyCount;
10
11 @property (strong, nonatomic) Goods *myGoods;
12
13 + (instancetype)goodsCellWithTableView:(UITableView *)tableView;
14 @end
15
16
17 #import "BWGoodsCell.h"
18 #import "Goods.h"
19
20 @interface BWGoodsCell ()
21
22 @end
23
24 @implementation BWGoodsCell
25
26 // 重写setMyGoods方法,把模型的数据设置给子控件
27 - (void)setMyGoods:(Goods *)myGoods
28 {
29     _myGoods = myGoods;
30
31     self.imgView.image = [UIImage imageNamed:_myGoods.icon];
32     self.lblName.text = _myGoods.title;
33     self.lblPrice.text = [NSString stringWithFormat:@"$ %@",_myGoods.price];
34     self.lblBuyCount.text = [NSString stringWithFormat:@"%@个人已购买",_myGoods.buyCount];
35 }
36
37 // 创建单元格
38 + (instancetype)goodsCellWithTableView:(UITableView *)tableView
39 {
40     static NSString *cellIndentifier = @"cellIndentifier";
41     BWGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
42
43     if (!cell) {
44         cell = [[[NSBundle mainBundle] loadNibNamed:@"BWGoodsCell" owner:nil options:nil] firstObject];
45     }
46     return cell;
47 }
48 - (void)awakeFromNib {
49     // Initialization code
50 }
51
52 - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
53     [super setSelected:selected animated:animated];
54
55     // Configure the view for the selected state
56 }
57
58 @end

三、Controller

  1 #import "ViewController.h"
  2 #import "Goods.h"
  3 #import "BWGoodsCell.h"
  4 #import "BWFooterView.h"
  5 #import "BWHeaderView.h"
  6
  7 @interface ViewController ()<UITableViewDataSource,BWFooterViewDelegate>
  8
  9 @property (nonatomic, strong) NSMutableArray *arrayModel;
 10 @property (nonatomic, strong) UITableView *tableView;
 11
 12 @end
 13
 14 @implementation ViewController
 15
 16 #pragma mark - 懒加载
 17 - (NSArray *)arrayModel
 18 {
 19     if (_arrayModel == nil) {
 20         NSString *path = [[NSBundle mainBundle] pathForResource:@"tgs.plist" ofType:nil];
 21
 22         NSArray *arrayDict = [NSArray arrayWithContentsOfFile:path];
 23
 24         NSMutableArray *arrayModel = [NSMutableArray array];
 25
 26         for (NSDictionary *dict in arrayDict) {
 27             Goods *goodsModel = [Goods goodsWithDict:dict];
 28             [arrayModel addObject:goodsModel];
 29         }
 30         _arrayModel = arrayModel;
 31     }
 32
 33     return _arrayModel;
 34 }
 35
 36 #pragma mark - 加载视图
 37 - (void)viewDidLoad {
 38     [super viewDidLoad];
 39     self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
 40     self.tableView.dataSource =self;
 41
 42     [self.view addSubview:self.tableView];
 43     self.tableView.rowHeight = 100;
 44
 45     //ps:tableView 的tableFooterView特点:只能修改x和height值,y和height不能修改
 46
 47     //创建tableFooterView
 48     BWFooterView *footerView = [BWFooterView footerView];
 49     //设置footerView的代理
 50     footerView.delegate =self;
 51     self.tableView.tableFooterView = footerView;
 52
 53     //创建tableHeaderView
 54     BWHeaderView *headerView = [BWHeaderView headerView];
 55
 56     self.tableView.tableHeaderView = headerView;
 57
 58
 59 }
 60 #pragma mark - CZFooterView的代理方法
 61 - (void)footerViewUpDateData:(BWFooterView *)footerView
 62 {
 63     //3.增加一条数据
 64     //3.1创建一个模型对象
 65     Goods *model = [[Goods alloc] init];
 66     model.title = @"驴肉火烧";
 67     model.price = @"6.0";
 68     model.buyCount = @"1000";
 69     model.icon = @"7003217f16ed29bab85e635a3bd6b60d";
 70     //3.2把模型对象加载控制器的goods集合中
 71     [self.arrayModel addObject:model];
 72     //4.刷新UITableView
 73     [self.tableView reloadData];
 74
 75     //ps:局部刷新(仅适用于UITableView的总行数没有发生变化的时候)
 76 //    NSIndexPath *indexpath =  [NSIndexPath indexPathForRow:self.arrayModel.count-1 inSection:0];
 77 //    [self.tableView reloadRowsAtIndexPaths:@[indexpath] withRowAnimation:UITableViewRowAnimationLeft];
 78
 79     //5.把UITableView中最后一行滚动到最上面
 80     NSIndexPath *indexpath =  [NSIndexPath indexPathForRow:self.arrayModel.count-1 inSection:0];
 81     [self.tableView scrollToRowAtIndexPath:indexpath atScrollPosition:UITableViewScrollPositionTop animated:YES];
 82 }
 83
 84 #pragma mark - 数据源
 85 //加载组的行数
 86 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 87 {
 88     return self.arrayModel.count;
 89 }
 90 //加载单元格数据
 91 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 92 {
 93     //1.获取数据模型
 94     Goods *goodsModel = self.arrayModel[indexPath.row];
 95
 96     //2.创建单元格
 97     BWGoodsCell *cell = [BWGoodsCell goodsCellWithTableView:tableView];
 98
 99 //   在控制器中直接为cell的每个子控件赋值数据造成问题
100 // 1>控制器强依赖于cell,一旦cell内部的子控件发生变化,那么子控件中的代码也得改(紧耦合)
101 // 2>cell封装不够完整,凡是用到cell的地方
102 // 3>解决:直接把模型传递给自定义cell,然后在自定义cell内部解析model中的数据赋值给自定义cell的内部的子控件
103
104     //3.把模型数据设置给单元格
105     cell.myGoods = goodsModel;
106
107     //4.返回单元格
108     return cell;
109 }
110
111 #pragma mark - 状态栏
112 - (BOOL)prefersStatusBarHidden
113 {
114     return YES;
115 }
116
117 #pragma mark - 内存
118 - (void)didReceiveMemoryWarning {
119     [super didReceiveMemoryWarning];
120     // Dispose of any resources that can be recreated.
121 }
122
123 @end

转载于:https://www.cnblogs.com/oc-bowen/p/5111213.html

iOS UI-团购案例(通过xib文件自定义UITableViewCell)相关推荐

  1. IOS开发基础之团购案例17-xib和UITableView两种方式实现

    IOS开发基础之团购案例17-xib和UITableView两种方式实现 Design By Johnson Shanghai 实现效果 系统和Xcode版本 注意的细节 关键性的代码 // // V ...

  2. 狮子鱼社区团购系统CMS任意文件2处上传漏洞

    漏洞复现 FOFA语句 "/seller.php?s=/Public/login" POC1 POST /Common/ckeditor/plugins/multiimg/dial ...

  3. **IOS:xib文件解析(xib和storyboard的比较,一个轻量级一个重量级)

    使用Xcode做iOS项目,经常会和Xib文件打交道,因为Xib文件直观的展现出运行时视图的外观,所以上手非常容易,使用也很方便,但对于从未用纯代码写过视图的童鞋,多数对Xib的理解有些片面. Xib ...

  4. Xib文件使用(一)——xib文件解析

    使用Xcode做iOS项目,经常会和Xib文件打交道,因为Xib文件直观的展现出运行时视图的外观,所以上手非常容易,使用也很方便,但对于从未用纯代码写过视图的童鞋,多数对Xib的理解有些片面. Xib ...

  5. iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局

    iOS开发UI篇-使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 iOS开发UI篇-使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目 ...

  6. IOS开发基础之UI基础的团购源码完整版本

    IOS开发基础之UI基础的团购源码完整版本 // // ViewController.m // 17-团购案例 // // Created by 鲁军 on 2021/2/4. //#import & ...

  7. ios 团购信息客户端demo(一)

    团购信息客户端,主要整合了ASIHTTPREQUEST,KISSXML,AQGridView,MBProgressHUD这几个主要流行的IOS开发库,我们先来看一下效果图 首先我们新建一个IOS工程, ...

  8. 仿团购app连接mysql_美团App(仿) - iOS开发

    模仿美团App 本项目是用Swift开发,StoryBoard 和 Xib 快速布局的. 这篇文是记录文,项目完成之后,再整理用到的知识,发一篇有条理的文 使用Xib开发的教程链接:xib使用教程 商 ...

  9. IOS 学习笔记 使用xib文件构建界面

    目录 使用xib文件构建界面 1.将ViewController.h.ViewController.m.Main.storyboard三个文件delete掉 2.Deployment Info的Mai ...

  10. ios 团购信息客户端demo(三)

    接上二篇的内容,今天我们就来介绍一下如何将解析出来的数据放入AQGridView中显示出来,因为我们的工程中已经将AQGridView导入了,所以我们在KKFirstViewController中直接 ...

最新文章

  1. Apache Thrift使用简介
  2. C++ STL 算法精选之查找篇
  3. Delphi读取文本内容
  4. tensorflow环境下的识别食物_在TensorFlow+Keras环境下使用RoI池化一步步实现注意力机制...
  5. 解决Firefox已阻止运行早期版本Adobe Flash
  6. Unity MegaFiers 顶点动画
  7. coreboot学习10:coreboot第一阶段学习小结
  8. HDOJ 2074 叠筐
  9. [Linked List]Intersection of Two Linked Lists
  10. qcloud-python-sts 下载安装
  11. 对比excel 轻松学python电子书_对比Excel,轻松学习Python数据分析
  12. 联想V470 安装win7系统 经验
  13. PROFINET协议
  14. 题6.12:有一行电文,已按照下面规律翻译成密码: A->Z a->z B->Y b->y C->X c->x即第1个字母编程第26个字母,第i个字母编程第(26-i+1)个字母,非字母字符不变,要求
  15. Docker直接删除elasticsearch报错:Failed to obtain node locks
  16. Android集成高德地图实现自定义Marker
  17. 云服务器进不了超星_超星自动答题搭建本地和云服务器题库(Java版)
  18. 【CS231n】斯坦福大学李飞飞视觉识别课程笔记(四):图像分类笔记(上)
  19. 快速填充空单元格-快速填充上一行或者下一行数据
  20. 最好的5个电脑上的epub阅读器

热门文章

  1. 世界主要城市地铁地图
  2. Nginx 静态页面POST 请求提示405 Not Allowed
  3. 在ClassWizard无法显示添加的类解决方法(转载)
  4. Internet Explorer更改MIME处理方式以提高安全性
  5. js获取浏览器和设备相关width(屏幕的宽度)
  6. js学习总结----浏览器滚动条卷去的高度scrolltop
  7. POJ 3279 Fliptile
  8. Trie树 01Trie
  9. jquery.serialize
  10. php常用加密函数总结