仿一号店APP商品分类效果开发IOS版本

jerry IOS 2015年11月27日 收藏

       好久没写博客了,一方面这段时间公司的事情项目比较多,另一方面自己也有准备成立自己的个人工作室,所以一直没顾得上去总结写一下技术博客,今天正好有点时间就来一篇吧。

       这段时间一直在进行开发商城的APP(Android和IOS),也研究一下国内绝大多数的电商APP(例如:淘宝,京东,天猫,苏宁易购,顺丰优选等等),他们的商品分类其实大多数属于常见格调:列表-点击二级列表-具体三级分类列表。唯有看了一号店IOS端的APP,感觉还不错。一级格子分布,点击item,中间显示二级分类。然后我们团队决定仿照该效果实现我们的APP。今天的文章打算分为两部分来写1.IOS端效果模拟。2.Android端效果模拟。

        首先我们看一下一号店IOS APP以及初步模拟的界面效果(当然现在是属于初版,很多细节问题还有待需改)

       

       下面来稍微讲解一下模拟实现这样效果的具体思路:

        ①:首先一级商品目录,我这边使用计算画出每一个View,可以说是九宫格,其实这边我们有12主商品分类,可以算是12宫格吧。

        ②:然后在点击每一个一级商品分类的时候,动态去判断添加已经删除二级分类view。

        ③:二级分类view,动态的放入了相应的商品分类button。

        ④:然后在进行切换显示二级分类View的时候,加入显示动画以及隐藏的view。

        好了基本的方法思路就是这么多功能啦。好了接下来是具体实现的主要代码,因为代码中还有很多冗余的地方,里面还有很多的问题,还没有具体的进行整理。希望大家不要喷我哦~后面会进一部优化的,然后会给出相对完善的代码。

        

       具体功能代码如下:

       (一):一级分类以及点击显示二级分类效果主要功能代码 ContentScrollView.h

  1. <span style="font-family:SimSun;font-size:18px;">//
  2. // ContentScrollView.h
  3. // zttmall
  4. //
  5. // Created by hmjiangqq on 15/1/7.
  6. // Copyright (c) 2015年 江清清<<a>http://www.chinaztt.com/</a>中天科技软件技术有限公司>. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10. #import "SubCategoryView.h"
  11. #define COLUMN_SIZE 4 //这边暂定为4行view
  12.  
  13. @interface ContentScrollView : UIScrollView
  14. {
  15. UIScrollView *_classifyScrollView;
  16. NSMutableArray *_lineViewArray; //每行view数组
  17. UITapGestureRecognizer *_tap;
  18. BOOL _toggle; //二级商品分类 页面打开以及关闭开关
  19. NSUInteger _transHeight; //平移的距离
  20. NSUInteger _preIndex; //前一次点击的item 索引
  21. NSUInteger _preColumn; //前一次点击的行号
  22. float mTopMark[COLUMN_SIZE]; //定义每一行的view的高度
  23. //二级商品分类view
  24. SubCategoryView *_subCategoryView;
  25. NSArray *_categoryData;
  26. }
  27. @property(nonatomic,retain)NSArray *data;
  28.  
  29. - (id)initWithFrame:(CGRect)frame;
  30.  
  31. @end
  32. </span>
        ContentScrollView.m

  1. <span style="font-family:SimSun;font-size:18px;">//
  2. // ContentScrollView.m
  3. // zttmall
  4. //
  5. // Created by hmjiangqq on 15/1/7.
  6. // Copyright (c) 2015年 江清清<<a>http://www.chinaztt.com/</a>中天科技软件技术有限公司>. All rights reserved.
  7. //
  8.  
  9. #import "ContentScrollView.h"
  10. #import "FirstCategoryItemModel.h"
  11. #import "UIImageView+WebCache.h"
  12. #import "RTLabel.h"
  13.  
  14. #define COLUMN_NUMBER 3
  15. #define TRANS_SPEED 0.4
  16.  
  17. //定义标志当前item是点在第一行,还是最后一行,以及中间位置,三种情况的界面的处理效果不一样
  18. //点击位置枚举
  19. enum{
  20. ClassifyClickLocationBegin,
  21. ClassifyClickLocationCenter,
  22. ClassifyClickLocationEnd
  23. } ENUM_LineClickOnLocation;
  24.  
  25. //定义标志前后两次点击的是否为同一个 old:表示前后两次点击item一样,new:表示前后两次点击item不一样,还有就是不同行
  26. enum{
  27. CLassifyItemClickOld,
  28. CLassifyItemClickNew,
  29. ClassifyItemClickNoSameColumn
  30. } ENUM_LineItemClickMark;
  31.  
  32. @implementation ContentScrollView
  33.  
  34. -(id)initWithFrame:(CGRect)frame{
  35. self=[super initWithFrame:frame];
  36. if(self){
  37. //初始化scrollView
  38. _classifyScrollView = [[UIScrollView alloc]initWithFrame:frame];
  39. _classifyScrollView.directionalLockEnabled = YES;//锁定滑动的方向
  40. _classifyScrollView.pagingEnabled = YES;//滑到subview的边界
  41. _classifyScrollView.bounces=NO;
  42. _classifyScrollView.bouncesZoom=NO;
  43. _classifyScrollView.alwaysBounceVertical=NO;
  44. _classifyScrollView.alwaysBounceHorizontal=NO;
  45. _classifyScrollView.showsVerticalScrollIndicator = NO; //不显示垂直滚动条
  46. _classifyScrollView.showsHorizontalScrollIndicator = NO;//不显示水平滚动条
  47. [self addSubview:_classifyScrollView];
  48. //设置默认的item点击平台开关以及平移的距离
  49. _toggle=NO;
  50. //设置默认的item前后两次点击效果不一样的为new
  51. ENUM_LineItemClickMark=CLassifyItemClickNew;
  52. //设置默认的前一次点击的索引为-1
  53. _preIndex=-1;
  54. //设置默认的前一次点击的行号索引为-1
  55. _preColumn=-1;
  56. }
  57. return self;
  58. }
  59.  
  60. /**
  61. * 进行一级分类数据设置
  62. * 1.以及分类中 每一个小方块为106x106 还要加上右边以及下边的1px细线 那就为107x107
  63. *
  64. * @param data
  65. */
  66. -(void)setData:(NSArray *)data{
  67. if(_data!=data){
  68. [_data release];
  69. _data=[data retain];
  70. }
  71. NSUInteger count=data.count/COLUMN_NUMBER;
  72. _lineViewArray=[[NSMutableArray arrayWithCapacity:count]retain];
  73. CGSize newSize = CGSizeMake(ScreenWidth, 107*count); //设置scrollview的大小
  74. [self setContentSize:newSize];
  75. for(int i=0;i<count;i++){
  76. UIView *_lineView=[[UIView alloc]initWithFrame:CGRectMake(0, i*107, ScreenWidth, 107)];
  77. //每行中的每列分类(每行有三列以及两条垂直细线)
  78. for(int j=0;j<5;j++){
  79. if(j==0){
  80. int index=3*i+j;
  81. FirstCategoryItemModel *firstItemModel=[data objectAtIndex:index];
  82. //第一个方格
  83. UIImageView *_oneItem=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 106, 106)];
  84. _oneItem.userInteractionEnabled=YES;
  85. _oneItem.tag=index;
  86. _tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(_classifyItemClick:)];
  87. [_oneItem addGestureRecognizer:_tap];
  88. [_tap release];
  89. [self _addClassifyIcon:firstItemModel.icon withInView:_oneItem];
  90. [self _addClassifyTitle:firstItemModel.title withInView:_oneItem];
  91. [_lineView addSubview:_oneItem];
  92. [_oneItem release];
  93. }else if (j==1){
  94. //第一条细线
  95. UIImageView *_oneLine=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"line_shu.png"]];
  96. _oneLine.frame=CGRectMake(106, 0, 1, 107);
  97. [_lineView addSubview:_oneLine];
  98. [_oneLine release];
  99. }else if (j==2){
  100. //第二个方格
  101. int index=3*i+j-1;
  102. FirstCategoryItemModel *firstItemModel=[data objectAtIndex:index];
  103. UIImageView *_towItem=[[UIImageView alloc]initWithFrame:CGRectMake(107, 0, 106, 106)];
  104. _towItem.userInteractionEnabled=YES;
  105. _towItem.tag=index;
  106. _tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(_classifyItemClick:)];
  107. [_towItem addGestureRecognizer:_tap];
  108. [_tap release];
  109. [self _addClassifyIcon:firstItemModel.icon withInView:_towItem];
  110. [self _addClassifyTitle:firstItemModel.title withInView:_towItem];
  111. [_lineView addSubview:_towItem];
  112. [_towItem release];
  113. }else if (j==3){
  114. //第二条细线
  115. UIImageView *_twoLine=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"line_shu.png"]];
  116. _twoLine.frame=CGRectMake(106+107, 0, 1, 107);
  117. [_lineView addSubview:_twoLine];
  118. [_twoLine release];
  119. }else if (j==4){
  120. //第三个方格
  121. int index=3*i+j-2;
  122. FirstCategoryItemModel *firstItemModel=[data objectAtIndex:index];
  123. UIImageView *_threeItem=[[UIImageView alloc]initWithFrame:CGRectMake(214, 0, 106, 106)];
  124. _threeItem.userInteractionEnabled=YES;
  125. _threeItem.tag=index;
  126. _tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(_classifyItemClick:)];
  127. [_threeItem addGestureRecognizer:_tap];
  128. [_tap release];
  129. [self _addClassifyIcon:firstItemModel.icon withInView:_threeItem];
  130. [self _addClassifyTitle:firstItemModel.title withInView:_threeItem];
  131. [_lineView addSubview:_threeItem];
  132. [_threeItem release];
  133. }
  134. }
  135. //每行底部的细线
  136. UIImageView *_bottomImage=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"line_read_option.png"]];
  137. _bottomImage.frame=CGRectMake(0, 106, ScreenWidth, 1);
  138. [_lineView addSubview:_bottomImage];
  139. [_bottomImage release];
  140. [self addSubview:_lineView];
  141. //没一行分类view加入到数组进行保存起来,以便接下来点击切换View
  142. [_lineViewArray addObject:_lineView];
  143. //记录每一行的位置
  144. mTopMark[i]=_lineView.top;
  145. [_lineView release];
  146. }
  147.  
  148. }
  149. /**
  150. * 使用商品一级分类的图标以及要添加上上的view 构造UIImageView
  151. *
  152. * @param icon 以及分类的图标地址
  153. * @param inView
  154. */
  155. -(void)_addClassifyIcon:(NSString *)icon withInView:(UIImageView *)inView{
  156. UIImageView *_item=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 70, 70)];
  157. [_item setImageWithURL:[NSURL URLWithString:icon] placeholderImage:[UIImage imageNamed:@"ic_classify_default.png"]];
  158. _item.top=8;
  159. _item.left=(106-70)/2;
  160. [inView addSubview:_item];
  161. [_item release];
  162. }
  163. /**
  164. * 使用商品分类名称以及要添加在上面的view 构造UILable
  165. *
  166. * @param title 商品分类名称
  167. * @param inView 被添加在上面的view
  168. */
  169. -(void)_addClassifyTitle:(NSString *)title withInView:(UIImageView *)inView{
  170. RTLabel *_itemLable=[[RTLabel alloc]initWithFrame:CGRectMake(0, 88, 100, 15)];
  171. [_itemLable setText:title];
  172. [_itemLable setTextColor:[UIColor blackColor]];
  173. _itemLable.font=[UIFont systemFontOfSize:12.0f];
  174. [_itemLable sizeToFit];
  175. _itemLable.top=85;
  176. _itemLable.left=(106-_itemLable.optimumSize.width)/2;
  177. [inView addSubview:_itemLable];
  178. [_itemLable release];
  179. }
  180.  
  181.  
  182. /**
  183. * 一级分类 item点击效果
  184. *
  185. * @param tap 传入的手势
  186. */
  187. -(void)_classifyItemClick:(UITapGestureRecognizer *)pTap{
  188. UIImageView *_itemView=(UIImageView *)pTap.view;
  189. //根据点击的item的索引来判断是第几行view,然后进行移动其他的行view
  190. NSUInteger index=_itemView.tag;
  191. NSUInteger column=index/3;
  192. NSUInteger count= [_lineViewArray count];
  193. [self _JudgeMark:index];
  194. //进行设置二级分类数据界面效果以及相对应的数据传入
  195. FirstCategoryItemModel *_firstModel=[self.data objectAtIndex:index];
  196. _transHeight =[self _getSubViewHeigth:_firstModel.sub_category];
  197. [self _addSubContentView:index withFirstData:_firstModel withHeigth:_transHeight];
  198. if(ENUM_LineClickOnLocation==ClassifyClickLocationBegin){
  199. if(ENUM_LineItemClickMark==CLassifyItemClickOld){
  200. [self _animationTransBegin:_transHeight withColumn:column withCount:count withLineViews:_lineViewArray];
  201. }else if (ENUM_LineItemClickMark==CLassifyItemClickNew){
  202. [self _animationTransBackSuit:_lineViewArray];
  203. [self _animationTransBegin:_transHeight withColumn:column withCount:count withLineViews:_lineViewArray];
  204. [self _addSubContentView:index withFirstData:_firstModel withHeigth:_transHeight];
  205.  
  206. }else if (ENUM_LineItemClickMark==ClassifyItemClickNoSameColumn){
  207. //点击不同行
  208. //1.首先把原来的行view全部回归原位
  209. //2.然后进行平移相关的行view
  210. [self _animationTransBackSuit:_lineViewArray];
  211. [self _animationTransBegin:_transHeight withColumn:column withCount:count withLineViews:_lineViewArray];
  212. [self _addSubContentView:index withFirstData:_firstModel withHeigth:_transHeight];
  213.  
  214. }
  215. }else if (ENUM_LineClickOnLocation==ClassifyClickLocationCenter){
  216. if(ENUM_LineItemClickMark==CLassifyItemClickOld){
  217. [self _animationTransCenter:_transHeight withColumn:column withCount:count withLineViews:_lineViewArray];
  218. }else if (ENUM_LineItemClickMark==CLassifyItemClickNew){
  219. [self _animationTransBackSuit:_lineViewArray];
  220. [self _animationTransCenter:_transHeight withColumn:column withCount:count withLineViews:_lineViewArray];
  221. [self _addSubContentView:index withFirstData:_firstModel withHeigth:_transHeight];
  222.  
  223. }else if (ENUM_LineItemClickMark==ClassifyItemClickNoSameColumn){
  224. //点击不同行
  225. [self _animationTransBackSuit:_lineViewArray];
  226. [self _animationTransCenter:_transHeight withColumn:column withCount:count withLineViews:_lineViewArray];
  227. [self _addSubContentView:index withFirstData:_firstModel withHeigth:_transHeight];
  228.  
  229. }
  230. }else if (ENUM_LineClickOnLocation==ClassifyClickLocationEnd){
  231. if(ENUM_LineItemClickMark==CLassifyItemClickOld){
  232. [self _animationTransEnd:_transHeight withColumn:column withLineViews:_lineViewArray];
  233. }else if (ENUM_LineItemClickMark==CLassifyItemClickNew){
  234. [self _animationTransBackSuit:_lineViewArray];
  235. [self _animationTransEnd:_transHeight withColumn:column withLineViews:_lineViewArray];
  236. [self _addSubContentView:index withFirstData:_firstModel withHeigth:_transHeight];
  237.  
  238. }else if (ENUM_LineItemClickMark==ClassifyItemClickNoSameColumn){
  239. //点击不同行
  240. [self _animationTransBackSuit:_lineViewArray];
  241. [self _animationTransEnd:_transHeight withColumn:column withLineViews:_lineViewArray];
  242. [self _addSubContentView:index withFirstData:_firstModel withHeigth:_transHeight];
  243.  
  244. }
  245. }
  246. }
  247.  
  248. /**
  249. * 根据点击的索引 来进行计算一些枚举值效果
  250. *
  251. * @param index 点击item的效果
  252. */
  253. -(void)_JudgeMark:(NSUInteger)index {
  254. //设置当前的点击的行号
  255. NSUInteger mTemp=index/3;
  256. if(_preColumn!=mTemp){
  257. ENUM_LineItemClickMark=ClassifyItemClickNoSameColumn; //点击不同行
  258. }else{
  259. if(_preIndex==index){
  260. //表示两次点击同一个item
  261. ENUM_LineItemClickMark=CLassifyItemClickOld;
  262. }else{
  263. //表示两次不同一个item
  264. ENUM_LineItemClickMark=CLassifyItemClickNew;
  265. }
  266. }
  267. //记录下当前点击的item索引以及点击行的行号
  268. _preIndex=index;
  269. _preColumn=mTemp;
  270. //判断点击的位置 第一行,最后一行,中间行
  271. if(index<3){
  272. ENUM_LineClickOnLocation=ClassifyClickLocationBegin;
  273. }else if (index<9){
  274. ENUM_LineClickOnLocation=ClassifyClickLocationCenter;
  275. }else{
  276. ENUM_LineClickOnLocation=ClassifyClickLocationEnd;
  277. }
  278. }
  279.  
  280. /**
  281. * 点击第一行 view平移的动画效果
  282. *
  283. * @param pTransHeight 平移的距离
  284. * @param pColumn 当前点击的item 在行的索引
  285. * @param pCount 所有行数
  286. */
  287. -(void)_animationTransBegin:(NSInteger) pTransHeight withColumn:(NSInteger) pColumn withCount:(NSUInteger)pCount withLineViews:(NSArray *)pLineViews{
  288. if(_toggle){
  289. // //进行关闭
  290. // for(int i=pColumn+1;i<pCount;i++){
  291. // UIView *_lineItemView=[_lineViewArray objectAtIndex:i];
  292. // [UIView animateWithDuration:TRANS_SPEED animations:^{
  293. // _lineItemView.top=_lineItemView.top-pTransHeight;
  294. // } completion:^(BOOL finished) {
  295. //
  296. // }];
  297. // }
  298. [self _animationTransBackSuit:pLineViews];
  299. _toggle=false;
  300. }else{
  301. //进行打开
  302. for(NSUInteger i=pColumn+1;i<pCount;i++){
  303. UIView *_lineItemView=[_lineViewArray objectAtIndex:i];
  304. [UIView animateWithDuration:TRANS_SPEED animations:^{
  305. _lineItemView.bottom=_lineItemView.bottom+pTransHeight;
  306. } completion:^(BOOL finished) {
  307. }];
  308. }
  309. _toggle=true;
  310. }
  311. }
  312. /**
  313. * 点击中间 view平移的动画效果
  314. *
  315. * @param pTransHeight 平移的距离
  316. * @param pColumn 当前点击的item 在行的索引
  317. * @param pCount 所有行数
  318. */
  319. -(void)_animationTransCenter:(NSInteger) pTransHeight withColumn:(NSInteger) pColumn withCount:(NSUInteger)pCount withLineViews:(NSArray *)pLineViews{
  320. if(_toggle){
  321. //进行关闭
  322. // for(int i=pColumn+1;i<pCount;i++){
  323. // UIView *_lineItemView=[_lineViewArray objectAtIndex:i];
  324. // [UIView animateWithDuration:TRANS_SPEED animations:^{
  325. // _lineItemView.top=_lineItemView.top-pTransHeight;
  326. // } completion:^(BOOL finished) {
  327. //
  328. // }];
  329. // }
  330. [self _animationTransBackSuit:pLineViews];
  331. _toggle=false;
  332. }else{
  333. //进行打开
  334. for(int i=pColumn+1;i<pCount;i++){
  335. UIView *_lineItemView=[_lineViewArray objectAtIndex:i];
  336. [UIView animateWithDuration:TRANS_SPEED animations:^{
  337. _lineItemView.bottom=_lineItemView.bottom+pTransHeight;
  338. } completion:^(BOOL finished) {
  339. }];
  340. }
  341. _toggle=true;
  342. }
  343. }
  344. /**
  345. * 点击最后一行 view平移的动画效果
  346. *
  347. * @param pTransHeight 平移的距离
  348. * @param pColumn 当前点击的item 在行的索引
  349. * @param pCount 所有行数
  350. */
  351. -(void)_animationTransEnd:(NSInteger) pTransHeight withColumn:(NSInteger) pColumn withLineViews:(NSArray *)pLineViews{
  352. if(_toggle){
  353. //进行关闭
  354. // for(int i=0;i<pColumn+1;i++){
  355. // UIView *_lineItemView=[_lineViewArray objectAtIndex:i];
  356. // [UIView animateWithDuration:TRANS_SPEED animations:^{
  357. // _lineItemView.top=_lineItemView.top+pTransHeight;
  358. // } completion:^(BOOL finished) {
  359. //
  360. // }];
  361. // }
  362. [self _animationTransBackSuit:pLineViews];
  363. _toggle=false;
  364. }else{
  365. //进行打开
  366. for(int i=0;i<pColumn+1;i++){
  367. UIView *_lineItemView=[_lineViewArray objectAtIndex:i];
  368. [UIView animateWithDuration:TRANS_SPEED animations:^{
  369. _lineItemView.top=_lineItemView.top-pTransHeight;
  370. } completion:^(BOOL finished) {
  371. }];
  372. }
  373. _toggle=true;
  374. }
  375. }
  376.  
  377. /**
  378. * 进行所有行回归原位
  379. *
  380. * @param _pLineViews 所有行view的数组
  381. */
  382. -(void)_animationTransBackSuit:(NSArray *)_pLineViews{
  383. if(_toggle){
  384. int count=_pLineViews.count;
  385. for(int i=0;i<count;i++){
  386. UIView *_lineItemView=[_pLineViews objectAtIndex:i];
  387. [UIView animateWithDuration:TRANS_SPEED animations:^{
  388. _lineItemView.top=mTopMark[i];
  389. } completion:^(BOOL finished) {
  390. }];
  391. }
  392. [self _removeSubCategoryView];
  393. _toggle=false;
  394. }
  395. }
  396.  
  397. /**
  398. * 清除二级目录视图
  399. */
  400. -(void)_removeSubCategoryView{
  401. if([_subCategoryView superview]!=nil){
  402. [_subCategoryView removeFromSuperview];
  403. }
  404. }
  405. /**
  406. * 进行添加二级分类数据view
  407. *
  408. * @param pClickItem 点击一级分类的item索引
  409. */
  410. -(void)_addSubContentView:(NSUInteger)pClickItem withFirstData:(FirstCategoryItemModel *)pFirstModel withHeigth:(NSUInteger) height{
  411. if([_subCategoryView superview]!=nil){
  412. [_subCategoryView removeFromSuperview];
  413. }
  414. _subCategoryView=[[SubCategoryView alloc]initWithFrame:CGRectMake(0, (pClickItem/3+1)*106+5, ScreenWidth, height)];
  415. [_subCategoryView setSub_category:pFirstModel.sub_category];
  416. [self addSubview:_subCategoryView];
  417. //[_subCategoryView release];
  418. }
  419.  
  420. /**
  421. * 计算二级分类商品view的高度
  422. *
  423. * @param pSubCategory 二级商品
  424. *
  425. * @return
  426. */
  427. -(int)_getSubViewHeigth:(NSArray *)pSubCategory{
  428. if(pSubCategory!=nil&&pSubCategory.count>0){
  429. int count=pSubCategory.count/3;
  430. return 20+count*10+(count+1)*30;
  431. }else{
  432. return 0;
  433. }
  434. }
  435.  
  436. -(void)dealloc{
  437. [_tap release];
  438. [_lineViewArray release];
  439. [super dealloc];
  440. }
  441. @end
  442. </span>
      (二):二级分类效果view  SubCategoryView.h

  1. <span style="font-family:SimSun;font-size:18px;">//
  2. // SubCategoryView.h
  3. // zttmall
  4. // 二级分类数据显示view
  5. // Created by hmjiangqq on 15/1/8.
  6. // Copyright (c) 2015年 江清清<<a>http://www.chinaztt.com/</a>中天科技软件技术有限公司>. All rights reserved.
  7. //
  8.  
  9. #import <UIKit/UIKit.h>
  10.  
  11. @interface SubCategoryView : UIView
  12. - (id)initWithFrame:(CGRect)frame;
  13.  
  14. @property(nonatomic,retain)NSArray *sub_category; //二级分类数据
  15.  
  16. @end
  17. </span>
        SubCategoryView.m

  1. <span style="font-family:SimSun;font-size:18px;">//
  2. // SubCategoryView.m
  3. // zttmall
  4. //
  5. // Created by hmjiangqq on 15/1/8.
  6. // Copyright (c) 2015年 江清清<<a>http://www.chinaztt.com/</a>中天科技软件技术有限公司>. All rights reserved.
  7. //
  8.  
  9. #import "SubCategoryView.h"
  10. #import "SubCategoryItemModel.h"
  11. @implementation SubCategoryView
  12.  
  13. -(id)initWithFrame:(CGRect)frame{
  14. self=[super initWithFrame:frame];
  15. if(self){
  16. [self _initView];
  17. }
  18. return self;
  19. }
  20. /**
  21. * 进行显示二级商品分类view
  22. */
  23. -(void)_initView{
  24. [self setBackgroundColor:Color(240, 240, 240, 1)];
  25. }
  26. /**
  27. * 进行把二级商品分类数据 添加在view中
  28. *
  29. * @param sub_category 二级商品分类的数据集合
  30. */
  31. -(void)setSub_category:(NSArray *)sub_category{
  32. if(_sub_category!=sub_category){
  33. [_sub_category release];
  34. _sub_category=[sub_category retain];
  35. }
  36. NSUInteger count=sub_category.count;
  37. for(NSUInteger i=0;i<count;i++){
  38. SubCategoryItemModel *subModel=[sub_category objectAtIndex:i];
  39. UIButton *_itemButton=[UIButton buttonWithType:UIButtonTypeCustom];
  40. _itemButton.frame=CGRectMake(107*(i%3)+10, 40*(i/3)+10, 87, 30);
  41. [_itemButton setBackgroundColor:[UIColor redColor]];
  42. [_itemButton setTitle:subModel.title forState:UIControlStateNormal];
  43. _itemButton.font=[UIFont systemFontOfSize:12.0f];
  44. [_itemButton addTarget:self action:@selector(_itemClick:) forControlEvents:UIControlEventTouchUpInside];
  45. [self addSubview:_itemButton];
  46. }
  47. }
  48. /**
  49. * 二级分类item 点击处理事件
  50. *
  51. * @param button
  52. */
  53. -(void)_itemClick:(UIButton *)button{
  54. NSLog(@"_itemClick...");
  55. }
  56. @end
  57. </span>
     (三):使用方法:只要在外部需要使用该控件地方声明,初始化,传入分类数据就可以了。

  1. <span style="font-family:SimSun;font-size:18px;">ContentScrollView *_contentScrollView=[[ContentScrollView alloc]initWithFrame:CGRectMake(0, 44, ScreenWidth, ScreenHeight-44)];
  2. [self.view addSubview:_contentScrollView];
  3. ShowCategoryModel *showModel=[CategoryDataParse parseCategoryData:dic];
  4. [_contentScrollView setData:showModel.data];
  5. </span>
    (四):我这边使用是我自己模拟的数据,所以下面是解析的工具类方法(JSON)   

  1. <span style="font-family:SimSun;font-size:18px;">//
  2. // CategoryDataParse.m
  3. // zttmall
  4. //
  5. // Created by hmjiangqq on 15/1/6.
  6. // Copyright (c) 2015年 江清清<<a>http://www.chinaztt.com/</a>中天科技软件技术有限公司>. All rights reserved.
  7. //
  8.  
  9. #import "CategoryDataParse.h"
  10. #import "FirstCategoryItemModel.h"
  11. #import "SubCategoryItemModel.h"
  12. #import "CategoryItemModel.h"
  13. #import "BrandItemModel.h"
  14. @implementation CategoryDataParse
  15.  
  16. +(ShowCategoryModel *)parseCategoryData:(NSDictionary *)dic{
  17. //分类商品总数据
  18. ShowCategoryModel *mShowCategoryModel=[[ShowCategoryModel alloc]init];
  19. mShowCategoryModel.api=[dic objectForKey:@"api"];
  20. mShowCategoryModel.v=[dic objectForKey:@"v"];
  21. mShowCategoryModel.code=[dic objectForKey:@"code"];
  22. mShowCategoryModel.msg=[dic objectForKey:@"msg"];
  23. //一级分类数据
  24. NSDictionary *datalist=[dic objectForKey:@"data"];
  25. NSMutableArray *data=[NSMutableArray array];
  26. id value;
  27. NSEnumerator *enumerator= [datalist objectEnumerator];
  28. while((value =[enumerator nextObject])!=nil){
  29. FirstCategoryItemModel *firstModel=[[FirstCategoryItemModel alloc]init];
  30. firstModel.cid=[value objectForKey:@"cid"];
  31. firstModel.title=[value objectForKey:@"title"];
  32. firstModel.desc=[value objectForKey:@"desc"];
  33. firstModel.icon=[value objectForKey:@"icon"];
  34. firstModel.linker=[value objectForKey:@"linker"];
  35. firstModel.category=[value objectForKey:@"category"];
  36. //二级分类数据
  37. NSDictionary *subCategoryList=[value objectForKey:@"sub_category"];
  38. NSMutableArray *subCategoryData=[NSMutableArray array];
  39. id pValue;
  40. NSEnumerator *pEnumerator= [subCategoryList objectEnumerator];
  41. while((pValue =[pEnumerator nextObject])!=nil){
  42. SubCategoryItemModel *mSubCategoryModel=[[SubCategoryItemModel alloc]init];
  43. mSubCategoryModel.cid=[pValue objectForKey:@"cid"];
  44. mSubCategoryModel.title=[pValue objectForKey:@"title"];
  45. mSubCategoryModel.desc=[pValue objectForKey:@"desc"];
  46. mSubCategoryModel.icon=[pValue objectForKey:@"icon"];
  47. mSubCategoryModel.linker=[pValue objectForKey:@"linker"];
  48. mSubCategoryModel.category=[pValue objectForKey:@"category"];
  49. //三级分类数据
  50. NSDictionary *threeCategoryList=[pValue objectForKey:@"three_category"];
  51. NSMutableArray *threeCategoryData=[NSMutableArray array];
  52. id tValue;
  53. NSEnumerator *tEnumerator= [threeCategoryList objectEnumerator];
  54. while((tValue =[tEnumerator nextObject])!=nil){
  55. CategoryItemModel *threeCategoryItemModel=[[CategoryItemModel alloc]init];
  56. threeCategoryItemModel.cid=[tValue objectForKey:@"cid"];
  57. threeCategoryItemModel.title=[tValue objectForKey:@"title"];
  58. threeCategoryItemModel.desc=[tValue objectForKey:@"desc"];
  59. threeCategoryItemModel.icon=[tValue objectForKey:@"icon"];
  60. threeCategoryItemModel.linker=[tValue objectForKey:@"linker"];
  61. threeCategoryItemModel.category=[tValue objectForKey:@"category"];
  62. [threeCategoryData addObject:threeCategoryItemModel];
  63. [threeCategoryItemModel release];
  64. }
  65. mSubCategoryModel.three_category=threeCategoryData;
  66. [subCategoryData addObject:mSubCategoryModel];
  67. [mSubCategoryModel release];
  68. }
  69. firstModel.sub_category=subCategoryData;
  70. //品牌数据
  71. NSDictionary *brandDic=[value objectForKey:@"brand"];
  72. NSMutableArray *brandData=[NSMutableArray array];
  73. id bdValue;
  74. NSEnumerator *bdEnumerator= [brandDic objectEnumerator];
  75. while((bdValue =[bdEnumerator nextObject])!=nil){
  76. BrandItemModel *brand=[[BrandItemModel alloc]init];
  77. brand.brandId=[bdValue objectForKey:@"brandId"];
  78. brand.brandTitle=[bdValue objectForKey:@"brandTitle"];
  79. brand.brandDesc=[bdValue objectForKey:@"brandDesc"];
  80. brand.brandIcon=[bdValue objectForKey:@"brandIcon"];
  81. brand.brandType=[bdValue objectForKey:@"brandType"];
  82. brand.brandLinker=[bdValue objectForKey:@"brandLinker"];
  83. [brandData addObject:brand];
  84. [brand release];
  85. }
  86. firstModel.brand=brandData;
  87. [data addObject:firstModel];
  88. [firstModel release];
  89. }
  90. mShowCategoryModel.data=data;
  91. return [mShowCategoryModel autorelease];
  92. }
  93. @end
  94. </span>
 相应的信息实体类(Model)就不贴了,直接看数据解析工具类,相信大家就能猜出来了。

下面给一下测试的地址:http://img2.xxh.cc:8080/SalesWebTest/CategoryList

好了IOS版本的基本模拟的讲解就到此,下一篇我会进行讲解Android版本的。

最后再说一下最近我自己在搞工作室,希望小编(如果小编看到这段觉得不妥,请帮我删掉这段,非常感谢!)以及路过的朋友们不要介意哦。优清科技工作室团队,主要承接Android/IOS,J2EE,.Net,PHP,移动建站,网站建设,微信系统项目开发。