博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS UI-团购案例(通过xib文件自定义UITableViewCell)
阅读量:6959 次
发布时间:2019-06-27

本文共 8531 字,大约阅读时间需要 28 分钟。

一、Model

1 #import 
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 @end15 16 #import "Goods.h"17 18 @implementation Goods19 20 - (instancetype)initWithDict:(NSDictionary *)dict21 {22 if (self = [super init]) {23 [self setValuesForKeysWithDictionary:dict];24 }25 return self;26 }27 28 + (instancetype)goodsWithDict:(NSDictionary *)dict29 {30 return [[self alloc] initWithDict:dict];31 }32 33 @end

二、View

1 #import 
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 @end15 16 @implementation BWHeaderView17 18 //当这个方法被执行的时候就表示BWHeaderView已经从xib文件中创建好了19 //BWHeaderView的子控件也都创建好了,所以就可以使用UIScrollView了。20 - (void)awakeFromNib21 {22 23 }24 25 //创建headerView26 + (instancetype)headerView27 {28 BWHeaderView *headerView = [[[NSBundle mainBundle] loadNibNamed:@"BWHeaderView" owner:nil options:nil] lastObject];29 return headerView;30 }31 32 @end

 

 

1 #import 
2 3 // 协议命名规范: 4 // 类名 + Delegate 5 // 协议中的方法最好加@optional 6 // 定义一个delegate属性,delegate属性用weak 7 // delegate属性声明为id类型,可以用来解除对头文件的依赖 8 9 @class BWFooterView;10 @protocol BWFooterViewDelegate
11 12 @required13 - (void)footerViewUpDateData:(BWFooterView *)footerView;14 @end15 16 @interface BWFooterView : UIView17 18 @property(weak, nonatomic) id
delegate;19 20 + (instancetype)footerView;21 22 @end23 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 @end32 33 @implementation BWFooterView34 35 //通过xib设置tableView中的tableFooterView36 + (instancetype)footerView37 {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.显示“等待指示器”所在的那个View48 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.刷新UITableView57 [self.delegate footerViewUpDateData:self];58 }59 //5.显示“加载更多”按钮60 self.btnLoadMore.hidden = NO;61 //6.隐藏“等待指示器”所在的那个View62 self.waittingView.hidden = YES;63 });64 65 }66 @end

 

1 #import 
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 @end15 16 17 #import "BWGoodsCell.h"18 #import "Goods.h"19 20 @interface BWGoodsCell ()21 22 @end23 24 @implementation BWGoodsCell25 26 // 重写setMyGoods方法,把模型的数据设置给子控件27 - (void)setMyGoods:(Goods *)myGoods28 {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 *)tableView39 {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 code50 }51 52 - (void)setSelected:(BOOL)selected animated:(BOOL)animated {53 [super setSelected:selected animated:animated];54 55 // Configure the view for the selected state56 }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 ()
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)prefersStatusBarHidden113 {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

 

转载地址:http://xwmil.baihongyu.com/

你可能感兴趣的文章
Prometheus vs. Graphite:时序数据监控工具选择
查看>>
Flask入门的第一个项目
查看>>
MikroTik RouterOS旧版本下载地址收集
查看>>
嵌入式软件开发工程师谈软件架构的设计
查看>>
./configure、make、make install 命令详解
查看>>
Laravel Response
查看>>
【Java入门提高篇】Day24 Java容器类详解(七)HashMap源码分析(下)
查看>>
[20180417]使用10046事件需要什么权限.txt
查看>>
如果centos7添加新网卡,系统不识别的解决办法
查看>>
JMX监控zookeeper
查看>>
Anaconda 2019.03 发布,Python 跨平台科学计算软件
查看>>
JS中遍历语法的比较
查看>>
读书笔记 effective c++ Item 15 在资源管理类中提供对原生(raw)资源的访问
查看>>
《C++ Primer》学习笔记:3.3.3其他vector操作
查看>>
NetBeans的(默认)快捷键
查看>>
年薪1美金还打N份工,我的收入终于超过马云了
查看>>
中国实体新气象:失之电商,收之“+互联网”
查看>>
W3C 宣布:WebAuthn 成为正式 Web 标准
查看>>
智能机器人登陆eSmart!或制造玩具市场新“痛点”
查看>>
调查显示,大多数 Java 开发人员不希望学习新语言
查看>>