一、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)的更多相关文章

  1. iOS开发——UI进阶篇(二)自定义等高cell,xib自定义等高的cell,Autolayout布局子控件,团购案例

    一.纯代码自定义等高cell 首先创建一个继承UITableViewCell的类@interface XMGTgCell : UITableViewCell在该类中依次做一下操作1.添加子控件 - ( ...

  2. ios – 使用UINib加载xib文件实现UITableViewCell

    xib文件的实质是xml,描述界面对象,每个对象都有一个很重要的属性,identity inspector面板中class属性,加载xib文件的时候实际上是实例化界面对象相对应的这些class. xi ...

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

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

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

    iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...

  5. iOS UI基础-9.1 UITableView 团购

    概述 接下来,我们要做的是团购界面的设计,最张要实现的效果图及项目结构图      团购数据的展示 思路: 系统自带的tableCell不能展示三个文本,不能满足条件,自定义tableCell 每一个 ...

  6. IOS第八天(2:UITableViewController团购,点击底部,xib加载更多, 代理模式)

    ******* HMViewController.h #import "HMViewController.h" #import "HMTg.h" #import ...

  7. iOS UI基础-4.2应用程序管理 Xib文件使用

    Xib调整使用 1.新建xib文件 New File-->User Interface-->Empty 2.打开新建的xib文件,出现可视化窗口 (1)拖入一个UIView (不是UIVi ...

  8. iOS开发——UI基础-自定义构造方法,layoutSubviews,Xib文件,利用Xib自定义View

    一.自定义构造方法 有时候需要快速创建对象,可以自定义构造方法 + (instancetype)shopView { return [[self alloc] init]; } - (instance ...

  9. IOS开发中UI编写方式——code vs. xib vs.StoryBoard

    最近接触了几个刚入门的iOS学习者,他们之中存在一个普遍和困惑和疑问,就是应该如何制作UI界面.iOS应用是非常重视用户体验的,可以说绝大多数的应用成功与否与交互设计以及UI是否漂亮易用有着非常大的关 ...

随机推荐

  1. UVA644 Immediate Decodability

    UVA644 Immediate Decodability Trie Trie模板题 难度几乎相等的题P2580 于是他错误的点名开始了 对于每组数据都清空树太浪费时间,所以我们只要在需要新点时预先把 ...

  2. ”MySQL查询优化“学习总结

    查询优化有几种方法,下面分别介绍. 切分查询 一条大的语句(涉及很多行)一次会锁住很多数据(不利于高并发). 占满整个事务日志,耗尽系统资源.阻塞很多小的但很重要的查询. 分解关联查询 关联查询分解方 ...

  3. 20145336 张子扬 《网络对抗技术》 web安全基础实践

    2014536 张子扬<网络攻防>Exp9 Web安全基础实践 实验准备 开启webgoat 1)开启webgoat,打开WebGoat: java -jar webgoat-contai ...

  4. cron表达式增加一段时间变为新的表达式

    cron表达式是使用任务调度经常使用的表达式了.对于通常的简单任务,我们只需要一条cron表达式就能满足.但是有的时候任务也可以很复杂. 最近我遇到了一个问题,一条任务在开始的时候要触发A方法,在结束 ...

  5. __NSCFConstantString && __NSPlaceholderDictionary

    一 -[__NSCFConstantString size]: unrecognized selector sent to instance 0x53ea70 该错误是在我将NSString类型的参数 ...

  6. python程序转为exe文件

    python开发者向普通windows用户分享程序,要给程序加图形化的界面(传送门:这可能是最好玩的python GUI入门实例! http://www.jianshu.com/p/8abcf73ad ...

  7. C# 将文件转换为 Stream

    public Stream FileToStream(string fileName) { // 打开文件 FileStream fileStream = new FileStream(fileNam ...

  8. BZOJ2563: 阿狸和桃子的游戏 贪心

    Description 阿狸和桃子正在玩一个游戏,游戏是在一个带权图G=(V, E)上进行的,设节点权值为w(v),边权为c(e).游戏规则是这样的: 1. 阿狸和桃子轮流将图中的顶点染色,阿狸会将顶 ...

  9. [bzoj 1260][CQOI 2007]涂色paint

    Description 假设你有一条长度为5的木版,初始时没有涂过任何颜色.你希望把它的5个单位长度分别涂上红.绿.蓝.绿.红色,用一个长度为5的字符串表示这个目标:RGBGR. 每次你可以把一段连续 ...

  10. Package.json 属性说明

    name - 包名. version - 包的版本号. description - 包的描述. entry pointer 项目入口文件 没有的直接回车跳过 test command: 测试命令 后面 ...