Purchase购物车实例分析
源代码下载地址:http://code4app.com/ios/55655def933bf09d328b5141
此源代码从中学到以下四个知识点
第一:关于右边只有一个被选中的效果展现,左边部分代码内容,其是一个列表的形式,橙色为一个视图的效果,当没有被选中时则隐藏起来,这边默认第一个被选中;
单元格BADockCell.h #import <UIKit/UIKit.h>
@interface BADockCell : UITableViewCell
@property (nonatomic ,weak) NSString *categoryText;
@property (nonatomic ,weak) UILabel *category;
@property (nonatomic ,weak) UIView *viewShow1;
+ (instancetype)cellWithTableView:(UITableView *)tableView;
@end .m文件内容: #import "BADockCell.h"
#import "Header.h"
@interface BADockCell ()
@end @implementation BADockCell -(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self =[super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
UILabel *category =[[UILabel alloc]initWithFrame:(CGRect){,,,}];
[self.contentView addSubview:category];
_category=category; UIView *viewShow =[[UIView alloc]initWithFrame:(CGRect){,49.5,,0.5}];
viewShow.backgroundColor=[UIColor blackColor];
viewShow.alpha=0.4;
[self.contentView addSubview:viewShow];
UIView *viewShow1 =[[UIView alloc]initWithFrame:(CGRect){,,,}];
viewShow1.backgroundColor=UIColorRGBA(, , , );
[self.contentView addSubview:viewShow1]; viewShow1.hidden=YES;
_viewShow1=viewShow1;
}
return self;
} -(void)setCategoryText:(NSString *)categoryText
{
_category.text=categoryText;
_category.textAlignment=NSTextAlignmentCenter;
_category.font=Font(); } + (instancetype)cellWithTableView:(UITableView *)tableView
{
static NSString *ID = @"BADockCell";
BADockCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[BADockCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
//取消选中状态
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
return cell;
}
@end
注意:+ (instancetype)cellWithTableView:(UITableView *)tableView在这边对CELL进行注册,然后在UITableView外面加载列时进行调用BADockCell *cell =[BADockCell cellWithTableView:tableView];
. h文件的内容(继承于UITableView) #import <UIKit/UIKit.h>
@protocol DockTavleViewDelegate <NSObject>
-(void)dockClickindexPathRow:(NSMutableArray *)row index:(NSIndexPath *)index indeXPath:(NSIndexPath *)indexPath;
@end @interface BAWineShoppingDockTavleView : UITableView
@property (nonatomic ,strong) NSMutableArray *dockArray;
@property (weak ,nonatomic) id <DockTavleViewDelegate>dockDelegate;
@end .m文件的内容 #import "BAWineShoppingDockTavleView.h"
#import "BADockCell.h"
#import "Header.h"
@interface BAWineShoppingDockTavleView ()<UITableViewDelegate,UITableViewDataSource> @property (nonatomic ,strong) NSIndexPath *path;
@property (nonatomic ,assign) BOOL is;
@end @implementation BAWineShoppingDockTavleView -(id)initWithFrame:(CGRect)frame
{
self =[super initWithFrame:frame];
if (self) {
self.dataSource=self;
self.delegate=self;
}
return self;
} -(void)layoutSubviews
{
[super layoutSubviews];
//只让它在初始化运行 运行一次_is就不为空了 给表格进行绑定
if(!_is)
{
NSInteger selectedIndex = ;
NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:selectedIndex inSection:];
[self tableView:self didSelectRowAtIndexPath:selectedIndexPath];
_is=YES;
}
} -(void)setDockArray:(NSMutableArray *)dockArray
{
_dockArray=dockArray;
} - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _dockArray.count;
} - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//这样方式的加载内容不错
BADockCell *cell =[BADockCell cellWithTableView:tableView];
cell.categoryText=_dockArray[indexPath.row][@"dockName"];
cell.backgroundColor=[UIColor whiteColor];
return cell;
} -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (_path!=nil) {
NSLog(@"上一个被选中%zd",_path.row);
BADockCell *cell = (BADockCell *)[tableView cellForRowAtIndexPath:_path];
cell.backgroundColor=[UIColor whiteColor];
cell.category.textColor=[UIColor blackColor];
cell.viewShow1.hidden=YES;
}
if ([_dockDelegate respondsToSelector:@selector(dockClickindexPathRow: index: indeXPath:)]) {
[_dockDelegate dockClickindexPathRow:_dockArray[indexPath.row][@"right"] index:_path indeXPath:indexPath];
}
//取消选中颜色 BADockCell *cell = (BADockCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.category.textColor=UIColorRGBA(, , , );
cell.backgroundColor=UIColorRGBA(, , , );
cell.viewShow1.hidden=NO;
_path=indexPath;
NSLog(@"选中%zd",_path.row);
}
@end
注意:对于_is变量只是用于第一次默认加载时,判断它并让其对第一个进行选中,最后进行赋值,这样就可以避免在表格绑定列时再进行判断的问题,而_path变量则是为了把选中的值赋给它,在didSelectRowAtIndexPath中一开始对它进行是否为空的判断,这样就可以清理掉上一次的选中;
第二:关于左边选中后右边进行更新,它还有一个效果是右边会刷新从顶部回到第一条,代码简单如下
@property (nonatomic ,strong) BAWineShoppingDockTavleView *dockTavleView;
@property (nonatomic ,strong) BARightTableView *rightTableView;
@property (nonatomic ,strong) NSMutableArray *dockArray;
@property (nonatomic ,strong) NSMutableArray *offsArray; -(void)viewDidLoad
{
BAWineShoppingDockTavleView *dockTavleView =[[BAWineShoppingDockTavleView alloc]initWithFrame:(CGRect){,,,kWindowHeight-}];
dockTavleView.rowHeight=;
dockTavleView.dockDelegate=self;
dockTavleView.backgroundColor=UIColorRGBA(, , , );
[dockTavleView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[self.view addSubview:dockTavleView];
_dockTavleView =dockTavleView; BARightTableView *rightTableView =[[BARightTableView alloc]initWithFrame:(CGRect){,,kWindowWidth-,kWindowHeight-}];
rightTableView.rowHeight=;
rightTableView.rightDelegate=self;
rightTableView.backgroundColor=UIColorRGBA(, , , );
[rightTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[self.view addSubview:rightTableView];
_rightTableView=rightTableView;
} -(void)dockClickindexPathRow:(NSMutableArray *)array index:(NSIndexPath *)index indeXPath:(NSIndexPath *)indexPath
{
[_rightTableView setContentOffset:_rightTableView.contentOffset animated:NO];
_offsArray[index.row] =NSStringFromCGPoint(_rightTableView.contentOffset);
_rightTableView.rightArray=array;
[_rightTableView reloadData];
CGPoint point=CGPointFromString([_offsArray objectAtIndex:indexPath.row]);
[_rightTableView setContentOffset:point];
}
第三:关于右边列增加或减少数量时,下面部分变化则是运用的回调的方式实现;在单元列中定义的一个回调,在右边的表格上进行一个委托,然后在主页面上进行实现变化的内容;
右边列.h文件: #import <UIKit/UIKit.h> @interface BARightCell : UITableViewCell
@property (nonatomic ,strong) NSMutableDictionary *rightData;
+ (instancetype)cellWithTableView:(UITableView *)tableView;
@property (nonatomic , copy) void (^TapActionBlock)(NSInteger pageIndex ,NSInteger money ,NSString *key);
@end 右边列.m文件(主要代码): -(void)wineRightClick
{
int NumberInt =[_wineQuantity.text intValue];
if (NumberInt ==) {
return;
}
++NumberInt; _wineQuantity.text =[NSString stringWithFormat:@"%d",NumberInt];
_rightData[@"Quantity"] = _wineQuantity.text;
_TapActionBlock([ _rightData[@"Quantity"] integerValue],[_rightData[@"money"] integerValue] ,_rightData[@"ProductID"]);
} -(void)wineLeftClick
{
int NumberInt =[_wineQuantity.text intValue];
if (NumberInt ==) {
return;
}
--NumberInt;
_wineQuantity.text =[NSString stringWithFormat:@"%d",NumberInt];
_rightData[@"Quantity"] = _wineQuantity.text;
_TapActionBlock([ _rightData[@"Quantity"] integerValue],[_rightData[@"money"] integerValue],_rightData[@"ProductID"]);
if (NumberInt ==) {
return;
} } 右边表格的.h: @protocol RightTableViewDelegate <NSObject>
-(void)quantity:(NSInteger)quantity money:(NSInteger)money key:(NSString *)key;
@end
@interface BARightTableView : UITableView
@property (nonatomic ,strong) NSMutableArray *rightArray;
@property (nonatomic ,weak) id<RightTableViewDelegate>rightDelegate;
@end 右边表格的.m: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BARightCell *cell =[BARightCell cellWithTableView:tableView];
cell.TapActionBlock=^(NSInteger pageIndex ,NSInteger money,NSString *key){
if ([self.rightDelegate respondsToSelector:@selector(quantity:money:key:)]) {
[self.rightDelegate quantity:pageIndex money:money key:key];
}
};
cell.backgroundColor=UIColorRGBA(, , , );
cell.rightData=_rightArray[indexPath.row];
return cell;
} 主页面实现在变化(rightTableView.rightDelegate=self): -(void)quantity:(NSInteger)quantity money:(NSInteger)money key:(NSString *)key
{
NSInteger addend =quantity *money; [_dic setObject:[NSString stringWithFormat:@"%ld",addend] forKey:key];
//得到词典中所有KEY值
NSEnumerator * enumeratorKey = [_dic keyEnumerator];
//遍历所有KEY的值
NSInteger total=;
NSInteger totalSingularInt=;
for (NSObject *object in enumeratorKey) {
total+=[_dic[object] integerValue];
if ([_dic[object] integerValue] !=) {
totalSingularInt +=;
_totalSingular.hidden=NO;
}
}
if (totalSingularInt==) {
_totalSingular.hidden=YES;
_bottomLabel.backgroundColor=[UIColor lightGrayColor];
_bottomLabel.userInteractionEnabled=NO;
_bottomLabel.text=@"请选购";
}else
{
_bottomLabel.backgroundColor=[UIColor clearColor];
_bottomLabel.userInteractionEnabled=YES;
_bottomLabel.text=@"去结算";
}
_totalSingular.text=[NSString stringWithFormat:@"%ld",totalSingularInt];
_totalPrice.text=[NSString stringWithFormat:@"¥%ld",total]; }
第四:关于页面的圆角样式效果
BALabel *bottomLabel =[[BALabel alloc]initWithFrame:(CGRect){kWindowWidth--,/-/,,}];
bottomLabel.text=@"请选购";
bottomLabel.textColor=[UIColor whiteColor];
bottomLabel.textAlignment=NSTextAlignmentCenter;
bottomLabel.font=Font();
bottomLabel.backgroundColor=[UIColor lightGrayColor];
bottomLabel.layer.masksToBounds=YES;
bottomLabel.layer.cornerRadius=;
bottomLabel.layer.borderWidth = ;
bottomLabel.userInteractionEnabled=NO;
[bottomLabel addTarget:self action:@selector(bottomLabelClick) forControlEvents:BALabelControlEventTap];
bottomLabel.layer.borderColor = [[UIColor whiteColor] CGColor];
[bottomView addSubview:bottomLabel];
Purchase购物车实例分析的更多相关文章
- 068——VUE中vuex的使用场景分析与state购物车实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- RPC原理及RPC实例分析
在学校期间大家都写过不少程序,比如写个hello world服务类,然后本地调用下,如下所示.这些程序的特点是服务消费方和服务提供方是本地调用关系. 1 2 3 4 5 6 public class ...
- java基础学习05(面向对象基础01--类实例分析)
面向对象基础01(类实例分析) 实现的目标 1.如何分析一个类(类的基本分析思路) 分析的思路 1.根据要求写出类所包含的属性2.所有的属性都必须进行封装(private)3.封装之后的属性通过set ...
- (转)实例分析:MySQL优化经验
[IT专家网独家]同时在线访问量继续增大,对于1G内存的服务器明显感觉到吃力,严重时甚至每天都会死机,或者时不时的服务器卡一下,这个问题曾经困扰了我半个多月.MySQL使用是很具伸缩性的算法,因此你通 ...
- sql注入实例分析
什么是SQL注入攻击?引用百度百科的解释: sql注入_百度百科: 所谓SQL注入,就是通过把SQL命令插入到Web表单提交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令.具 ...
- 实例分析ELF文件静态链接
参考文献: <ELF V1.2> <程序员的自我修养---链接.装载与库>第4章 静态链接 开发平台: [thm@tanghuimin static_link]$ uname ...
- 用实例分析H264 RTP payload
用实例分析H264 RTP payload H264的RTP中有三种不同的基本负载(Single NAL,Non-interleaved,Interleaved) 应用程序可以使用第一个字节来识别. ...
- nodejs的模块系统(实例分析exprots和module.exprots)
前言:工欲善其事,必先利其器.模块系统是nodejs组织管理代码的利器也是调用第三方代码的途径,本文将详细讲解nodejs的模块系统.在文章最后实例分析一下exprots和module.exprots ...
- Android Touch事件原理加实例分析
Android中有各种各样的事件,以响应用户的操作.这些事件可以分为按键事件和触屏事件.而Touch事件是触屏事件的基础事件,在进行Android开发时经常会用到,所以非常有必要深入理解它的原理机制. ...
随机推荐
- 一步一步实战扩展 ASP.NET Route,实现小写 URL、个性化 URL
介绍 不知道大家在使用 ASP.NET MVC 时有没有一些扩展要求,反正我是有很多.在使用 MVC 这几年(PS:我是从 1.0 开始学,2.0.3.0 开发至今),我深深地觉得 MVC 的扩展性真 ...
- css省略号布局实例截图
过多文字li标签出现使用css省略号样式截图 使用text-overflow样式让显示不完内容通过css实现省略号排版
- IIS6.0启动网站ManagedPipelineHandler异常
处理程序“PageHandlerFactory-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler” 开发web项目需要安装IIS,当安装完以后,web程 ...
- 调用Ajax返回500错误的解决方法
看代码: public ActionResult UserLogin(LogOnModel model) { #region 验证码验证 #endregion OperationResult resu ...
- 受限玻尔兹曼机(RBM)学习笔记(四)对数似然函数
去年 6 月份写的博文<Yusuke Sugomori 的 C 语言 Deep Learning 程序解读>是囫囵吞枣地读完一个关于 DBN 算法的开源代码后的笔记,当时对其中涉及的算 ...
- PintJS – 轻量,并发的 GruntJS 运行器
PintJS 是一个小型.异步的 GruntJS 运行器,试图解决大规模构建流程中的一些问题. 典型的Gruntfile 会包括 jsHint,jasmine,LESS,handlebars, ugl ...
- 解决安装office2013时提示已安装相同版本的office
例如出现如上所示的图: ------------------------------------------------------------------ 在尝试使用官方的卸载清理工具无果后, 终极 ...
- 2013/11/22工作随笔-缓存是放在Model层还是放在Controller层
web网站的典型代码框架就是MVC架构,Model层负责数据获取,Controller层负责逻辑控制,View层则负责展示. 一般数据获取是去mysql中获取数据 但是这里有个问题,我们不会每次请求都 ...
- [ShortCut] Visual Studio快捷键
msdn官方快捷键说明:https://msdn.microsoft.com/zh-cn/library/da5kh0wa.aspx 测试工具: visual studio 2013 操作步骤: 1. ...
- maven异常解决:编码GBK的不可映射字符
直接将项目改为UTF-8编码,无效!要通过修改pom.xml文件,告诉maven这个项目使用UTF-8来编译. 一.问题描述 今天在MyEclipse中使用Maven编译项目源代码时,结果如下了如下的 ...