• 通过懒加载把最初的plist文件加载后,根据plist文件文件中的目标控制器进行跳转,根据加载的plist文件中的plist_name加载将要跳转进去的控制器界面的控件等等.

  • 以上根据target_vc创建对应的目标控制器

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSDictionary * group = self.groups[indexPath.section];
NSDictionary * item = [group[@"items"] objectAtIndex:indexPath.row];
NSString * targetClassName = item[@"target_vc"];
if (targetClassName) {
//字符串转类
Class TargetClass = NSClassFromString(targetClassName);
//所有控制器都继承自UIViewControler用他来接收,并创建对应目标控制器类型
UIViewController * destVc = [[TargetClass alloc] init];
if ([destVc isKindOfClass:[PBBSettingViewController class]]) {
PBBSettingViewController * settingVc = (PBBSettingViewController *)destVc; //如果点击的cell可以跳转新的"设置控制器"则,将group懒加载的self.plistname修改为要跳转的plist文件名,在这里则根据key--plist_name键取相应的plist文件名,进行跳转准备,准备内部控件
settingVc.plistName = item[@"plist_name"]; }
destVc.navigationItem.title = item[@"title"];
[self.navigationController pushViewController:destVc animated:YES];
} }
  • 如果目标控制器是类似设置的控制器,则强转为设置类型的控制器,由于继承其所有方法与属性,则可以加载对应的plist文件对齐内部控件进行赋值等操作.

  • 看图分析plist文件层级结构

  • 首先整个plist是个数组,数组内部对应的成员都是字典,字典内部对应的tableView的每组的成员 有 header 和 footer 以及内部的cell,所以字典内部存放的是以字典存储的header和footer以及以数组存放的cell们,每个cell又是一个字典用来取对应的图片资源 或者 创建对应的目标控制器以及目标plist文件.

  • 本文几乎全部是tableview,则通过懒加载来获取plist文件数据.通过数据源方法来对获取的数据进行解析--对控件进行创建以及赋值.

以下代码是控制代码,用来获取plist文件,把通过plist文件获取的资源赋给对应的tableView的控件



#import "PBBSettingViewController.h"
#import "PBBSettingCell.h" @interface PBBSettingViewController () @property(nonatomic,strong)NSArray * groups; @end @implementation PBBSettingViewController -(NSArray *)groups{ if (_groups == nil) {
NSString * path = [[NSBundle mainBundle] pathForResource:self.plistName ofType:nil];
_groups = [NSArray arrayWithContentsOfFile:path];
}
return _groups;
} #pragma mark - 数据源
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//多少个组
return self.groups.count;
} -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ //先获取组
NSDictionary * dict = self.groups[section];
//获取组中对应items的数量 -- 每组cell的数量
NSArray * arr = dict[@"items"];
return arr.count;
} -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSDictionary * group = self.groups[indexPath.section];
NSDictionary * item = [group[@"items"] objectAtIndex:indexPath.row]; //由于跳转的控制器的tableView的cell的sytle不同则在cell内部封装判断cell的style的方法.并且需要在编写plist文件中写上cell的style用来用key键取值去判断相应cell的style.item是获取一组cell中单个的cell以及属性. PBBSettingCell * cell = [PBBSettingCell settingCellWith:tableView item:item]; //调用了item的set方法,在PBBSettingCell.m文件中实现了item的set方法,在set方法中进行了对cell内部子控件(左边icon头像的判断,中间title,以及details,右边accessory的判断以及赋值)的赋值.
cell.item = item;
return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSDictionary * group = self.groups[indexPath.section];
NSDictionary * item = [group[@"items"] objectAtIndex:indexPath.row];
NSString * targetClassName = item[@"target_vc"];
if (targetClassName) {
//字符串转类
Class TargetClass = NSClassFromString(targetClassName);
//所有控制器都继承自UIViewControler用他来接收,并创建对应目标控制器类型
UIViewController * destVc = [[TargetClass alloc] init];
if ([destVc isKindOfClass:[PBBSettingViewController class]]) {
PBBSettingViewController * settingVc = (PBBSettingViewController *)destVc; //如果点击的cell可以跳转新的"设置控制器"则,将group懒加载的self.plistname修改为要跳转的plist文件名,在这里则根据key--plist_name键取相应的plist文件名,进行跳转准备,准备内部控件
settingVc.plistName = item[@"plist_name"]; }
destVc.navigationItem.title = item[@"title"];
[self.navigationController pushViewController:destVc animated:YES];
} } -(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ NSDictionary * group = self.groups[section];
return group[@"footer"];
} -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ NSDictionary * group = self.groups[section];
return group[@"header"];
} //设置tableView为分组的样式,重写构造init方法
-(instancetype)init{ return [super initWithStyle:UITableViewStyleGrouped];
} -(instancetype)initWithStyle:(UITableViewStyle)style{ return [super initWithStyle:UITableViewStyleGrouped];
} //设置返回按钮 格式 不同状态下的显示图片
-(void)viewDidLoad{ [super viewDidLoad]; UIBarButtonItem * backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"NavBack"] style:UIBarButtonItemStylePlain target:self action:@selector(didClickBackButton)]; self.navigationItem.rightBarButtonItem.title = @"常见问题";
self.navigationItem.title = @"设置哦!";
self.navigationItem.leftBarButtonItem = backItem; }
//如果自定义了返回按钮,则返回按钮进入上一个控制器需要手动返回.
-(void)didClickBackButton{ [self.navigationController popViewControllerAnimated:YES]; } @end

以下是在cell.m文件中对传递进来的数据进行解析---创建控件---对控件进行赋值(控件头像,title,以及accessory类型等等的判断)


#import "PBBSettingCell.h" @implementation PBBSettingCell //通过plist文件中的cell_style来创建对应的cell的style
+(UITableViewCellStyle)cellStyleWithText:(NSString *)cellStyle{ if ([cellStyle isEqualToString:@"UITableViewCellStyleSubtitle"]) {
return UITableViewCellStyleSubtitle;
} else if ([cellStyle isEqualToString:@"UITableViewCellStyleValue2"]){ return UITableViewCellStyleValue2;
} else if([cellStyle isEqualToString:@"UITableViewCellStyleValue1"]){ return UITableViewCellStyleValue1;
} else {
return UITableViewCellStyleDefault;
}
} //这个有卵用??-------暂时没有用...
+(instancetype)settingCellWith:(UITableView *)tableView{ static NSString * ID = @"settigs_cell";
PBBSettingCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) {
cell = [[PBBSettingCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
} //颜色可以随便设置
cell.detailTextLabel.textColor = [UIColor colorWithRed:173.0/255.0 green:166.0/255.0 blue:144.0/255.0 alpha:1.0];
return cell;
}
+(instancetype)settingCellWith:(UITableView *)tableView item:(NSDictionary *)item{ static NSString * ID = @"settigs_cell";
PBBSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (cell == nil) { cell = [[PBBSettingCell alloc] initWithStyle:[self cellStyleWithText:item[@"cell_style"]] reuseIdentifier:ID];
}
cell.detailTextLabel.textColor = [UIColor colorWithRed:173.0/255.0 green:166.0/255.0 blue:144.0/255.0 alpha:1.0];
return cell;
} -(void)setItem:(NSDictionary *)item{ _item = item;
//判断是否有头像
if (item[@"icon"]) {
self.imageView.image = [UIImage imageNamed:item[@"icon"]];
} self.textLabel.text = item[@"title"];
self.detailTextLabel.text = item[@"details"];
//设置判断accessory
if (item[@"accessory"] && [item[@"accessory"] length]>0) {
//字符串转换为类
Class AccessoryClass = NSClassFromString(item[@"accessory"]);
id accessory_obj = [[AccessoryClass alloc] init];
//判断是否有图片框 没有图片框就是UISwitch类型或者是什么都没有...
if ([accessory_obj isKindOfClass:[UIImageView class]]) { UIImageView * imageView = (UIImageView *)accessory_obj;
imageView.image = [UIImage imageNamed:item[@"accessory_img"]];
[imageView sizeToFit];
}
self.accessoryView = accessory_obj;
} } - (void)awakeFromNib {
// Initialization code
} - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated]; // Configure the view for the selected state
} @end

总结:类似这种控制器的跳转,跳转后还需要进行跳转的"设置"类型的跳转,需要自己写plist文件的,而且跳转后的控制器的都基本是同一种控制器例如tableViewController,可以在plist文件中写入所有的tableView的属性,因为可能跳转进入不同的控制器界面对应的控件有所不同,有的就写上,创建什么类型的cell进行判断即可.

论--如何通过代码解析plist文件创建对应的控制器,以及控制器中的控件的更多相关文章

  1. C# WinForm中 让控件全屏显示的实现代码

    夏荣全 ( lyout(at)163.com )原文 C#中让控件全屏显示的实现代码(WinForm) 有时候需要让窗口中某一块的内容全屏显示,比如视频播放.地图等等.经过摸索,暂时发现两种可行方法, ...

  2. Android在代码中设置控件的drawableLeft,drawableRight,drawableTop,drawableBottom。

    根据业务的需要,要在代码中设置控件的drawableLeft,drawableRight,drawableTop,drawableBottom属性. 我们知道在xml中设置的方法为:android:d ...

  3. Webform中Repeater控件--绑定嵌入C#代码四种方式

    网页里面嵌入C#代码用的是<% %>,嵌入php代码<?php ?> 绑定数据的四种方式: 1.直接绑定 <%#Eval("Code") %> ...

  4. Qt中在UI文件中新建控件并命名,但在代码中无法识别UI中的控件?

    代码中添加FilePathLineEdit控件,显示标准文件选择对话框显示选择的文件路径,但在槽函数中ui->FilePathLineEdit->setText("FilePat ...

  5. C#代码中设置 控件的触发器

    Style style = new Style(); style.TargetType = typeof(TextBox); MultiDataTrigger trigger = new MultiD ...

  6. Android开发之常用框架WebView详解代码。超详细,送给初学者,完全掌握此控件

    这是我特意为新手小白写的一个代码,教大家完完全全掌握WebView, 我感觉,你看懂这个,基本上可以满足以后工作中的需要了,(只针对Webview的使用),但是其实它还有好多功能,比如真正的设计到和H ...

  7. iOS代码实现:创建按钮,绑定按钮事件,读取控件值

    // // main.m // Hello // // Created by lishujun on 14-8-28. // Copyright (c) 2014年 lishujun. All rig ...

  8. C#代码总结02---使用泛型来获取Asp前台页面全部控件,并进行属性修改

    该方法:主要用于对前台页面的不同类型(TextBox.DropDownList.等)或全部控件进行批量操作,用于批量修改其属性(如,Text.Enable). private void GetCont ...

  9. PHP代码中input控件使用id无法POST传值,使用name就可以

    <html> <head> <title>Example</title> </head> <body> <?php if ...

随机推荐

  1. Myeclipse 多版本共存

    Myeclipse2015 stable2.0 和Myeclipse10 共存的解决办法: 1. 准备内容: 1) myeclipse-2015-stable-2.0-offline-installe ...

  2. python实践3:cursor() — 数据库连接操作

    python 操作数据库,要安装一个Python和数据库交互的包MySQL-python-1.2.2.win32-py2.5.exe,然后我们就可以使用MySQLdb这个包进行数据库操作了. 操作步骤 ...

  3. functional cohesion

    Computer Science An Overview _J. Glenn Brookshear _11th Edition A weak form of cohesion is known as ...

  4. Flink - Juggling with Bits and Bytes

    http://www.36dsj.com/archives/33650 http://flink.apache.org/news/2015/05/11/Juggling-with-Bits-and-B ...

  5. 用c++builder读取一个一行有多行变量的文件

    文件内容如下: C DXDY.INP FILE, IN FREE FORMAT ACROSS COLUMNS for  83658 Active CellsC 2013-5-25   上午 10:43 ...

  6. 如何在外网中访问自己在另一个局域网中的某个机器(SSH为例)

    UBUNTU 14.04 LTS 为例 如何在外网中访问自己在另一个局域网中的某个机器(SSH为例) 2013-05-01 16:02 2693人阅读 评论(0) 收藏 举报 情景描述: 计算机C1放 ...

  7. python之列表切片(slice)

    使用索引获取列表的元素(随机读取) 列表元素支持用索引访问,正向索引从0开始 colors=["red","blue","green"] c ...

  8. 【Android测试】【第四节】LogCat——认识和使用

    ◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/4682321.html 什么是LogCat LogCat可以 ...

  9. 利用快速排序原理找出数组中前n大的数

    #include <stdio.h> #include <stdint.h> #include <stdlib.h> #define MAX_SIZE 400001 ...

  10. C++ 类成员函数作为参数

    #include <iostream> using namespace std; typedef int int32_t; struct IMsgBody{ int body; }; st ...