产品推荐

产品推荐使用的是UICollectionView控件,UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView 和 UITableViewController 类。

思路:

  1. 模型:建立一个MJProduct模型,存放产品的相关信息(标题、图标)
  2. 视图:创建xib,继承于UICollectionViewCell
  3. 控制器:创建MJProductViewController,继承于UICollectionViewController
  4. 数据读取通过product.json文件

模型创建

MJProduct.h

#import <Foundation/Foundation.h>

@interface MJProduct : NSObject
/** 标题 */
@property (nonatomic, copy) NSString *title;
/** 图标 */
@property (nonatomic, copy) NSString *icon; -(instancetype)initWithDict:(NSDictionary *)desc;
+(instancetype)productWithDict:(NSDictionary *)desc;
@end

MJProduct.m

//
// MJProduct.m
// Lottery
//
// Created by jiangys on 15/9/7.
// Copyright (c) 2015年 weconex. All rights reserved.
// #import "MJProduct.h" @implementation MJProduct -(instancetype)initWithDict:(NSDictionary *)desc
{
if (self=[super init]) {
self.title=desc[@"title"];
self.icon=desc[@"icon"];
}
return self;
} +(instancetype)productWithDict:(NSDictionary *)desc
{
return [[self alloc] initWithDict:desc];
} @end

视图xib创建

新建一个xib,分别拉入

CollectionViewCell,Class指定MJProductCell,Size选Freeform,大小80*80

ImageView ,大小55*50

Label,字体11px,大小80*24,居然显示

MJProductCell.h

//
// MJProductCell.h
// Lottery
//
// Created by jiangys on 15/9/7.
// Copyright (c) 2015年 weconex. All rights reserved.
// #import <Foundation/Foundation.h>
@class MJProduct; @interface MJProductCell : UICollectionViewCell
/** 产品模型 */
@property (nonatomic, strong) MJProduct *product;
@end

MJProductCell.m

//
// MJProductCell.m
// Lottery
//
// Created by jiangys on 15/9/7.
// Copyright (c) 2015年 weconex. All rights reserved.
// #import "MJProductCell.h"
#import "MJProduct.h"
@interface MJProductCell() @property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel; @end @implementation MJProductCell -(void)awakeFromNib
{
//设置图片圆角
self.iconView.layer.cornerRadius=;
self.iconView.clipsToBounds=YES;
} -(void)setProduct:(MJProduct *)product
{
_product=product;
self.iconView.image=[UIImage imageNamed:product.icon];
self.nameLabel.text=product.title;
}
@end

分别为imageview和Label拖线到MJProductCell.m中。

控制器实现

我们编写的product.json格式如下:

[
{
"title": "网易电影票",
"id": "com.netease.movie",
"url": "http://itunes.apple.com/app/id583784224?mt=8",
"icon": "movie",
"customUrl": "movieticket163"
},
{
"title": "网易新闻",
"id": "com.netease.news",
"url": "http://itunes.apple.com/app/id425349261?mt=8",
"icon": "newsapp",
"customUrl": "newsapp"
},
{
"title": "网易公开课",
"id": "com.netease.videoHD",
"url": "http://itunes.apple.com/app/id415424368?mt=8",
"icon": "open",
"customUrl": "ntesopen"
},
{
"title": "网易电视指南",
"id": "com.netease.tvguide",
"url": "http://itunes.apple.com/app/id480095986",
"icon": "tvguide",
"customUrl": "tvguide"
},
{
"title": "网易阅读",
"id": "com.netease.iphonereader",
"url": "http://itunes.apple.com/app/id462186890?mt=8",
"icon": "reader",
"customUrl": ""
},
{
"title": "网易云相册",
"id": "com.netease.cloudphotos",
"url": "http://itunes.apple.com/app/id451931841?mt=8",
"icon": "cloudphotos",
"customUrl": ""
},
{
"title": "网易手机邮",
"id": "com.netease.RPMMS-UI",
"url": "http://itunes.apple.com/app/id371634015?mt=8",
"icon": "shoujiyou",
"customUrl": ""
},
{
"title": "网易微博",
"id": "com.netease.microblogging",
"url": "http://itunes.apple.com/app/id383629309?mt=8",
"icon": "newb",
"customUrl": "microblogging"
},
{
"title": "有道词典",
"id": "youdaoPro",
"url": "http://itunes.apple.com/app/id353115739?mt=8",
"icon": "youdao",
"customUrl": "yddictProapp"
},
{
"title": "饭饭",
"id": "fanfan",
"url": "https://itunes.apple.com/cn/app/fan-fan/id487503086?mt=8",
"icon": "fanfan",
"customUrl": ""
},
{
"title": "有道笔记",
"id": "com.youdao.note.iphone",
"url": "http://itunes.apple.com/app/id450748070?mt=8",
"icon": "youdaonote",
"customUrl": ""
},
{
"title": "恵恵",
"id": "cn.huihui.Guide",
"url": "https://itunes.apple.com/cn/app/hui-hui-gou-wu-jin-nang-shang/id650705207?mt=8&ls=1",
"icon": "huihui",
"customUrl": ""
}
]

MJProductViewController.h

//
// MJProductViewController.h
// Lottery
//
// Created by jiangys on 15/9/7.
// Copyright (c) 2015年 weconex. All rights reserved.
// #import <UIKit/UIKit.h> @interface MJProductViewController : UICollectionViewController @end

MJProductViewController.m

//
// MJProductViewController.m
// Lottery
//
// Created by jiangys on 15/9/7.
// Copyright (c) 2015年 weconex. All rights reserved.
// #import "MJProductViewController.h"
#import "MJProduct.h"
#import "MJProductCell.h" @interface MJProductViewController ()
@property(nonatomic,strong) NSArray *products;
@end @implementation MJProductViewController static NSString * const reuseIdentifier = @"Cell"; -(NSArray *)products
{
if (_products==nil) {
// JSON文件的路径
NSString *path=[[NSBundle mainBundle] pathForResource:@"products.json" ofType:nil];
// 加载JSON文件
NSData *data=[NSData dataWithContentsOfFile:path];
// 将JSON数据转换成NSArray
NSArray *dictArray=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
// 将字典转为模型
NSMutableArray *productArray=[NSMutableArray array];
for (NSDictionary *dict in dictArray) {
MJProduct *p=[MJProduct productWithDict:dict];
[productArray addObject:p];
}
_products=productArray;
}
return _products;
} -(instancetype)init
{
// 1.流水布局
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
// 2.每个cell的尺寸
layout.itemSize = CGSizeMake(, );
// 3.设置cell之间的水平间距
layout.minimumInteritemSpacing = ;
// 4.设置cell之间的垂直间距
layout.minimumLineSpacing = ;
// 5.设置四周的内边距
layout.sectionInset = UIEdgeInsetsMake(layout.minimumLineSpacing, , , );
return [super initWithCollectionViewLayout:layout]; } - (void)viewDidLoad {
[super viewDidLoad]; // 1.注册cell(告诉collectionView将来创建怎样的cell)
UINib *nib=[UINib nibWithNibName:@"MJProductCell" bundle:nil];
[self.collectionView registerNib:nib forCellWithReuseIdentifier:@"Product"]; self.collectionView.backgroundColor=[UIColor whiteColor];
// Register cell classes
//[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier]; // Do any additional setup after loading the view.
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /*
#pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/ #pragma mark <UICollectionViewDataSource> - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.products.count;
} - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { MJProductCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Product" forIndexPath:indexPath];
// 传递模型
cell.product=self.products[indexPath.item];
return cell;
} // 代理,选中
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
MJProduct *p=self.products[indexPath.item];
NSLog(@"点击了 %@",p.title);
} @end

效果:

帮助

要实现的效果:

思路:

1.帮助列表使用加载json文件

2.显示帮助说明的页面,使用加载html显示

html.json文件

[
{
"title" : "如何领奖?",
"html" : "help.html",
"id" : "howtoprize"
},
{
"title" : "如何充值?",
"html" : "help.html",
"id" : "howtorecharge"
},
{
"title" : "如何提现?",
"html" : "help.html",
"id" : "howtowithdraw"
},
{
"title" : "如何购彩?",
"html" : "help.html",
"id" : "howtobuy"
},
{
"title" : "如何连续多期买同一注号码?",
"html" : "help.html",
"id" : "whatisfollowandtimes"
},
{
"title" : "如何跟别人合资买彩票?",
"html" : "help.html",
"id" : "howtogroupbuy"
},
{
"title" : "如何快速支付?",
"html" : "help.html",
"id" : "howtoquickpay"
},
{
"title" : "如何投注双色球",
"html" : "ssq_howto.html"
},
{
"title" : "如何投注大乐透",
"html" : "dlt_howto.html"
},
{
"title" : "如何投注11选5",
"html" : "y11_howto.html"
},
{
"title" : "如何投注广东11选5",
"html" : "gdy11_howto.html"
},
{
"title" : "如何投注老11选5",
"html" : "jxy11_howto.html"
},
{
"title" : "如何投注好运11选5",
"html" : "lk11_howto.html"
},
{
"title" : "如何投注快3",
"html" : "k3_howto.html"
},
{
"title" : "如何投注快2",
"html" : "k2_howto.html"
},
{
"title" : "如何投注竞彩足球",
"html" : "jczq_howto.html"
},
{
"title" : "如何投注竞彩篮球",
"html" : "jclq_howto.html"
},
{
"title" : "如何投注足球单场",
"html" : "bjdc_howto.html"
},
{
"title" : "如何投注3D",
"html" : "x3d_howto.html"
},
{
"title" : "如何投注老时时彩",
"html" : "ssc_howto.html"
},
{
"title" : "如何投注七星彩",
"html" : "qxc_howto.html"
},
{
"title" : "如何投注七乐彩",
"html" : "qlc_howto.html"
},
{
"title" : "如何投注排列5",
"html" : "pl5_howto.html"
},
{
"title" : "如何投注排列3",
"html" : "pl3_howto.html"
},
{
"title" : "如何投注快乐8",
"html" : "kl8_howto.html"
},
{
"title" : "如何投注新时时彩",
"html" : "jxssc_howto.html"
},
{
"title" : "如何投注胜负彩",
"html" : "sfc_howto.html"
},
{
"title" : "如何投注任选九",
"html" : "f9_howto.html"
}
]

建立显示帮助列表模型

MJHtml.h

#import <Foundation/Foundation.h>

@interface MJHtml : NSObject
/** ID */
@property (nonatomic, copy) NSString *ID;
/** 网页标题 */
@property (nonatomic, copy) NSString *title;
/** 网页文件名 */
@property (nonatomic, copy) NSString *html; -(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)htmlWithDict:(NSDictionary *)dict;
@end

MJHtml.m

#import "MJHtml.h"

@implementation MJHtml

-(instancetype)initWithDict:(NSDictionary *)dict
{
if (self=[super init]) {
self.html=dict[@"html"];
self.title=dict[@"title"];
self.ID=dict[@"id"];
}
return self;;
} +(instancetype)htmlWithDict:(NSDictionary *)dict
{ return [[self alloc]initWithDict:dict];
} @end

建立一个列表controller,显示帮助列表,继承于MJBaseSettingViewController

MJHelpViewController.h

#import "MJBaseSettingViewController.h"

@interface MJHelpViewController : MJBaseSettingViewController

@end

MJHelpViewController.m

//
// MJHelpViewController.m
// Lottery
//
// Created by apple on 15/9/13.
// Copyright (c) 2015年 weconex. All rights reserved.
// #import "MJHelpViewController.h"
#import "MJHtml.h"
#import "MJSettingItem.h"
#import "MJSettingArrowItem.h"
#import "MJSettingGroup.h"
#import "MJHtmlViewController.h"
#import "MJNavigationController.h" @interface MJHelpViewController ()
@property(nonatomic,strong)NSMutableArray *htmls;
@end @implementation MJHelpViewController -(NSMutableArray *)htmls
{
if (_htmls==nil) { // 不加这一句代码,htmls数据加载不出来
_htmls = [NSMutableArray array]; // json文件路径
NSString *path=[[NSBundle mainBundle] pathForResource:@"help.json" ofType:nil]; // 加载json文件
NSData *data=[NSData dataWithContentsOfFile:path]; // 将JSON数据转为NSArray
NSArray *dictArray=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil]; // 将字典转为模型
for (NSDictionary *dict in dictArray) {
MJHtml *html=[MJHtml htmlWithDict:dict];
[_htmls addObject:html];
}
}
return _htmls; } - (void)viewDidLoad {
[super viewDidLoad]; // 创建所有的item
NSMutableArray *items=[NSMutableArray array];
for (MJHtml *html in self.htmls) {
MJSettingItem *item=[MJSettingArrowItem itemWithTitle:html.title destVcClass:nil];
[items addObject:item];
} // 创建组
MJSettingGroup *group=[[MJSettingGroup alloc] init];
group.items=items;
[self.data addObject:group];
} // 重写跳转方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
MJHtmlViewController *htmlVc = [[MJHtmlViewController alloc] init];
// 页面传值
htmlVc.html = self.htmls[indexPath.row];
//使用自定义导航栏
MJNavigationController *nav = [[MJNavigationController alloc] initWithRootViewController:htmlVc];
[self presentViewController:nav animated:YES completion:nil];
} @end

最后,就是加载UIWebView,显示帮助信息

MJHtmlViewController.h

#import <UIKit/UIKit.h>
@class MJHtml; @interface MJHtmlViewController : UIViewController /** hmtl模型 */
@property (nonatomic, strong) MJHtml *html;
@end 

MJHtmlViewController.m

#import "MJHtmlViewController.h"
#import "MJHtml.h" @interface MJHtmlViewController ()<UIWebViewDelegate> @end @implementation MJHtmlViewController -(void)loadView
{
self.view=[[UIWebView alloc] init];
} - (void)viewDidLoad {
[super viewDidLoad]; //设置标题
self.title=self.html.title; UIWebView *webView=(UIWebView *)self.view;
webView.delegate=self; // 创建URL
NSURL *url=[[NSBundle mainBundle] URLForResource:self.html.html withExtension:nil]; //创建请求
NSURLRequest *request=[NSURLRequest requestWithURL:url]; // 发送请求加载网页
[webView loadRequest:request]; // 设置左上角的关闭按钮
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(close)];
} -(void)close
{
[self dismissViewControllerAnimated:YES completion:nil];
} /**
* 网页加载完毕的时候调用
*/
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
// 跳到id对应的网页标签 // 1.拼接Javacript代码
NSString *js = [NSString stringWithFormat:@"window.location.href = '#%@';", self.html.ID];
// 2.执行JavaScript代码
[webView stringByEvaluatingJavaScriptFromString:js];
} @end

系统源码下载:点击下载

iOS 网易彩票-5设置模块二的更多相关文章

  1. iOS 网易彩票-6设置模块三(常用小功能)

    该篇文章中,用到很多iOS开发过程中常用的小功能,当前只是将这些功能集成到网易彩票的设置中.iOS-常用小功能介绍,请参考我的另一篇文章: iOS 常用小功能 总结:http://www.cnblog ...

  2. iOS 网易彩票-4设置模块一

    概述 基本上,每一款APP都有相应的设置模块.怎么设置才能更灵活和通用呢,这也是大家一直思考的.下面说说在网易彩票中,设置模块的设置思想. 基本上有三种方案: static cell(呆板,完全没有动 ...

  3. iOS 网易彩票-1框架搭建

    仿网易彩票,最终要做成的效果如下: 一.分层搭建 1.新建一个项目,Lottery.只支持7.1以上坚屏. 2.将素材全部图片全部拉到相应的文件夹里. 3.选中Lottery--右键Show in F ...

  4. iOS 网易彩票-3常见设置

    Navigation导航设置 为了统一管理导航控制器,需要自定义导航控制器MJNavigationController,继承于UINavigationController.分别设置5个Navigati ...

  5. iOS 网易彩票-2框架搭建-代码重构

    在上一篇中,我们基本已经把整个框架都搭建出来了,下面进行代码重构一下. 思路: 导航按钮,按下时,会变灰,那是系统自带了,通过自定义UIButton,实现按下按钮立即切换效果. MJTabBarCon ...

  6. iOS菜鸟成长笔记(2)——网易彩票练习

    距离上一篇<第一个iOS应用>已经有一个多月了,今天来和大家一起学习和分享一下一个小练习<网易彩票> 首先我们向storyboard中拖入一个TabBarController和 ...

  7. iOS开发——实战总结OC篇&网易彩票开发知识点总结

    网易彩票开发知识点总结 关于网易彩票开发中遇到了不少的坑,弄了好久才弄懂,或者有些犹豫很久没用就不记得了,所以这里就总结了一下,希望以后不会忘记,就算忘记也能快速查看! /************** ...

  8. 再造轮子之网易彩票-第一季(IOS 篇 by sixleaves)

    前言 在网上看了别人做的模仿网易彩票的项目, 于是也跟着用自己的想法做了一篇.写这篇博客的目的, 在于UI综合的一次小练习, 同时总结和串联其各个控件之间的应用.封装思想等.考虑到有人上不了githu ...

  9. WPF仿网易云音乐系列(二、歌单创建窗口+登录设置模块)

    老衲牺牲午休时间写博客,都快把自己感动了,-_-!! 之前上一篇随笔,我看了下评论,有部分人说WPF已经凉凉了,这个我觉得,这只是一个达到自己目的的工具而已,只要自己能用这个工具,得心应手的做出自己想 ...

随机推荐

  1. C# 中对COOKIES的操作

    HttpUtility.UrlDecode HttpUtility.UrlEncode HttpContext.Current.Request.Cookies["UserCode" ...

  2. aws.s3的 upload 和putObject有什么区别

    相同点:上传或新增一个object : <template> <div class="page"> <!-- 参考:https://blog.csdn ...

  3. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十九:SDRAM模块② — 多字读写

    实验十九:SDRAM模块② — 多字读写 表示19.1 Mode Register的内容. Mode Register A12 A11 A10 A9 A8 A7 A6 A5 A4 A3 A2 A1 A ...

  4. 3-1 vue-resource基础介绍

    1.静态引用 <script src="https://cdn.bootcss.com/vue-resource/1.3.4/vue-resource.js">< ...

  5. WCF之HTTPS传输WebConfig配置

    <system.serviceModel> <client> <endpoint address="https://evitest.1010bao.com:88 ...

  6. Java虚拟机八 分析Java堆

    常见的内存溢出的原因及其解决思路 1.堆溢出: 由于大量的对象都直接分配在堆上,因此它最有可能发生溢出.因为大量对象占据了堆空间,而这些对象都持有强引用,导致无法回收,当对象大小之和大于堆空间时就会发 ...

  7. gnuplot生成gif动画

    最近有个任务需要生成一个动态变化的图,然后突然发现gnuplot竟然可以生成gif动画,当真是应正了博客Gnuplot surprising的子标题: I always tell myself: &q ...

  8. 小程序中navigator和wx.navigateTo,wx.redirectTo,wx.reLaunch,wx.switchTab,wx.navigateBack的用法

    如果用一句话来表明navigator和API中wx.系列的跳转有什么区别,那就是navigator是在wxml中用标签添加open-type属性来达到和wx.系列一样的效果. navigator的属性 ...

  9. Cloudstack云平台实践

    laaS平台的虚拟化 利用率高 资源整合 节约电能 节约空间 灾难恢复 CloudStack是一个开源的具有高可用性及扩展性的云计算平台.支持管理大部分主流的hypervisor,如KVM虚拟机,Xe ...

  10. poj1696 Space Ant【计算几何】

    含极角序排序模板.   Space Ant Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5334   Accepted:  ...