iOS UI-团购案例(通过xib文件自定义UITableViewCell)
一、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
iOS UI-团购案例(通过xib文件自定义UITableViewCell)的更多相关文章
- iOS开发——UI进阶篇(二)自定义等高cell,xib自定义等高的cell,Autolayout布局子控件,团购案例
一.纯代码自定义等高cell 首先创建一个继承UITableViewCell的类@interface XMGTgCell : UITableViewCell在该类中依次做一下操作1.添加子控件 - ( ...
- ios – 使用UINib加载xib文件实现UITableViewCell
xib文件的实质是xml,描述界面对象,每个对象都有一个很重要的属性,identity inspector面板中class属性,加载xib文件的时候实际上是实例化界面对象相对应的这些class. xi ...
- **IOS:xib文件解析(xib和storyboard的比较,一个轻量级一个重量级)
使用Xcode做iOS项目,经常会和Xib文件打交道,因为Xib文件直观的展现出运行时视图的外观,所以上手非常容易,使用也很方便,但对于从未用纯代码写过视图的童鞋,多数对Xib的理解有些片面. Xib ...
- iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...
- iOS UI基础-9.1 UITableView 团购
概述 接下来,我们要做的是团购界面的设计,最张要实现的效果图及项目结构图 团购数据的展示 思路: 系统自带的tableCell不能展示三个文本,不能满足条件,自定义tableCell 每一个 ...
- IOS第八天(2:UITableViewController团购,点击底部,xib加载更多, 代理模式)
******* HMViewController.h #import "HMViewController.h" #import "HMTg.h" #import ...
- iOS UI基础-4.2应用程序管理 Xib文件使用
Xib调整使用 1.新建xib文件 New File-->User Interface-->Empty 2.打开新建的xib文件,出现可视化窗口 (1)拖入一个UIView (不是UIVi ...
- iOS开发——UI基础-自定义构造方法,layoutSubviews,Xib文件,利用Xib自定义View
一.自定义构造方法 有时候需要快速创建对象,可以自定义构造方法 + (instancetype)shopView { return [[self alloc] init]; } - (instance ...
- IOS开发中UI编写方式——code vs. xib vs.StoryBoard
最近接触了几个刚入门的iOS学习者,他们之中存在一个普遍和困惑和疑问,就是应该如何制作UI界面.iOS应用是非常重视用户体验的,可以说绝大多数的应用成功与否与交互设计以及UI是否漂亮易用有着非常大的关 ...
随机推荐
- Python3基础 ** 幂运算 // 整除运算
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- CSS 常用语法与盒模型分析
CSS基础知识 CSS规则由两个主要的部分构成:选择器,以及一条或者多条声明 selector { property: value; property: value; ... property: va ...
- GNU C 中零长度的数组【转】
原文链接:http://www.cnblogs.com/dolphin0520/p/3752492.html 在标准C和C++中,长度为0的数组是被禁止使用的.不过在GNU C中,存在一个非常奇怪的用 ...
- Linux系统编程--文件描述符的复制dup()和dup2()【转】
本文转载自:http://blog.csdn.net/tennysonsky/article/details/45870459 dup() 和 dup2() 是两个非常有用的系统调用,都是用来复制一个 ...
- cygwin下使用apt-cyg安装新软件
1.获取 (记得先安装好git) git clone https://github.com/transcode-open/apt-cyg.git 2.安装apt-cyg cd apt-cyg chm ...
- 【第二十七章】 springboot + zipkin(brave-okhttp实现)
本文截取自:http://blog.csdn.net/liaokailin/article/details/52077620 一.前提 1.zipkin基本知识:附8 zipkin 2.启动zipki ...
- chromedriver下载安装
博主开发平台是win10,Python版本是3.6.最近需要用到chromedriver+selenium,下载好selenium后,pip install chromedriver,直接安装到pyt ...
- P4-Related Tools Installation
P4-Related Tools Installation 安装P4相关工具的步骤和说明. 本说明只适用于 Ubuntu 14.04 系统. 推荐安装的其他工具 mininet:SDN网络仿真工具 v ...
- C# String.Join 与 StringBuilder 对比,谁更快
String.Join 文档 StringBuilder 文档 这两天刷 Leedcode 做到一道 String 的题时突然想到这俩对比的问题,于是查了一下资料并简单对比了一下. 首先对于 ...
- 01_Flume基本架构及原理
Flume消息收集系统,在整个系统架构中的位置 Flume概况1) Apache软件基金会的顶级项目2)存在两个大的版本:Flume 0.9.x(Flume-OG,original generatio ...