网易彩票-我的彩票-设置

1. 新建Cocoa Touch Class(Class:HMSetingController;Subclass of:UITableViewController;Language:Objective-C)
路径:Classes->MYLottery(我的彩票)->Controller
- (void)viewDidLoad
{
//设置标题
self.navigationItem.title = @"设置";
//创建返回按钮
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"NavBack"] style:UIBarButtonItemStylePlain
targer:self action:@selector(backClick)];
//设置 leftbaritem
self.navigationItem.leftBarButtonItem = item;
}
//点击返回按钮事件
- (void)backClick
{
[self.navigationController popViewControllerAnimated:YES];
}
2. ”设置“按钮基本页面
”设置“按钮->拖线->HMMyLotteryController.m:implementation->
- (IBAction)settingClick:(id)sender
{
//跳转到设置页面(组类型TableView)
HMSettingController *setting = [[HMSettingController alloc] initWithStyle:UITableViewStyleGrouped];

[self.navigationContrller pushViewControll:setting animated:YES];
}
3. 创建plist数据
3.1 新建Property List(Save As:Setting.plist;Tags:空)
路径:Classes->MYLottery(我的彩票)->Other
Root:Array
Item 0:Dictionary
Items:Array
Item 0:Dictionary
targetVC:String 要跳转到的View Controller的名字
accessoryContent:String arrow_right
accessoryType:String UIImageView
title:String
icon:String
footer
header

4. plist懒加载
4.1 HMSettingController.m:interface->申明变量:@property (nonatomic, strong) NSArray *groups;
4.2 HMMyLotteryController.m:implementation->懒加载,代码如下:
- (NSArray *)groups {
if (!_groups) {
//获取路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"Setting" ofType:@"plist"];

//解析成数组
_groups = [NSArray arrayWithContentsOfFile:path];
}
return _groups;
}
5 数据源方法
5.1 HMMyLotteryController.m:implementation->代码如下:
//有多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.groups.count;
}
//每1组有多少行
- (NSInteger)tableView:(UITableView)tableView numberOfRowsInSection:(NSInteger)section
{
//获取组
NSDictionary *group = self.groups[section];
//获取当前组所有的cell信息
NSArray * items = group[@"items"];
return items.count;
}

6. cell长什么样
5.1 HMMyLotteryController.m:implementation->代码如下:
//cell长什么样
- (UITableViewCell *)tableView:(UITableViw *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//获取当前cell的信息
//1.获取组
NSDictionary *group = self.groups[indexPath.section];
//2.获取当前组的所有的cell信息
NSArray *items = group[@"items"];
//3.当前cell的信息
NSDictionary *item = items[indexPath.row];

static NSString *cellid = @"setting_cell";

//缓存池找
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];

if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
}

//赋值
cell.imageView.image = [UIImage imageNamed:item[@"icon"]];
cell.textLable.text = item[@"title"];

//根据字符生成对象
//获取accessoryType的字符串
NSString *accessoryType = item[@"accessoryType"];
//将字符串转换成类
Class Clz = NSClassFromString(accessoryType);
//获取accessoryType的对象
UIView *obj = [[Clz alloc] init];

//判断obj真实的类型
if ([obj isKingOfClass:[UIImageView class]]) {
//设置frame,强转为图片类型
UIImageView *imageView = (UIImageView *)obj;
imageView.image = [UIImage imageName:item[@"accessoryContent"];
[imageView sizeToFist];
}
cell.accessoryView = obj;

return cell;
}

7. 点击cell推出对应的界面
7.1 新建Cocoa Touch Class(Class:HMRedeemController;Subclass of:UIViewController;Language:Objective-C)
路径:Classes->MYLottery(我的彩票)->Controller->Setting
创建背景为紫色的View Controller,代码如下:
- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor purpleColor];
}
7.2 HMSettingController.m:implementation->代码如下:
//点击cell调用
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//获取组
NSDictionary *group = self.groups[indexPath.section];
//获取所有的cell
NSArray *items = group[@"items"];
//获取当前的cell信息
NSDictionary *item = items[indexPath.row];

if (item[@"targetVC"] && [item[@"targetVC"] length] > 0) {
//@"HMRedeemController"
NSString *targetVC = item[@"targetVC"];
//HMRedeemController
Class Clz = NSClassFromString(targetVC);
//HMRedeemController类型的对象
UIViewController *vc = [[Clz alloc] init];
vc.navigationItem.title = item[@"title"];
//跳转
[self.navigationController pushViewController:vc animated:YES];
}
}

猜你喜欢

转载自www.cnblogs.com/diexin/p/9910062.html