论--如何通过代码解析plist文件创建对应的控制器,以及控制器中的控件
- 通过懒加载把最初的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文件创建对应的控制器,以及控制器中的控件的更多相关文章
- C# WinForm中 让控件全屏显示的实现代码
夏荣全 ( lyout(at)163.com )原文 C#中让控件全屏显示的实现代码(WinForm) 有时候需要让窗口中某一块的内容全屏显示,比如视频播放.地图等等.经过摸索,暂时发现两种可行方法, ...
- Android在代码中设置控件的drawableLeft,drawableRight,drawableTop,drawableBottom。
根据业务的需要,要在代码中设置控件的drawableLeft,drawableRight,drawableTop,drawableBottom属性. 我们知道在xml中设置的方法为:android:d ...
- Webform中Repeater控件--绑定嵌入C#代码四种方式
网页里面嵌入C#代码用的是<% %>,嵌入php代码<?php ?> 绑定数据的四种方式: 1.直接绑定 <%#Eval("Code") %> ...
- Qt中在UI文件中新建控件并命名,但在代码中无法识别UI中的控件?
代码中添加FilePathLineEdit控件,显示标准文件选择对话框显示选择的文件路径,但在槽函数中ui->FilePathLineEdit->setText("FilePat ...
- C#代码中设置 控件的触发器
Style style = new Style(); style.TargetType = typeof(TextBox); MultiDataTrigger trigger = new MultiD ...
- Android开发之常用框架WebView详解代码。超详细,送给初学者,完全掌握此控件
这是我特意为新手小白写的一个代码,教大家完完全全掌握WebView, 我感觉,你看懂这个,基本上可以满足以后工作中的需要了,(只针对Webview的使用),但是其实它还有好多功能,比如真正的设计到和H ...
- iOS代码实现:创建按钮,绑定按钮事件,读取控件值
// // main.m // Hello // // Created by lishujun on 14-8-28. // Copyright (c) 2014年 lishujun. All rig ...
- C#代码总结02---使用泛型来获取Asp前台页面全部控件,并进行属性修改
该方法:主要用于对前台页面的不同类型(TextBox.DropDownList.等)或全部控件进行批量操作,用于批量修改其属性(如,Text.Enable). private void GetCont ...
- PHP代码中input控件使用id无法POST传值,使用name就可以
<html> <head> <title>Example</title> </head> <body> <?php if ...
随机推荐
- 深入说明HDR
http://wenku.baidu.com/link?url=xBdq0VRVi2t0x9uis3XfU_0mKf2eK0e6y_1hiSo7IWSWyUE8yAwaTJ60ZlxTzQf91VPf ...
- Apache Storm技术实战之1 -- WordCountTopology
欢迎转载,转载请注意出处,徽沪一郎. “源码走读系列”从代码层面分析了storm的具体实现,接下来通过具体的实例来说明storm的使用.因为目前storm已经正式迁移到Apache,文章系列也由twi ...
- How To Ask Questions The Smart Way
How To Ask Questions The Smart Way Eric Steven Raymond Thyrsus Enterprises <esr@thyrsus.com> R ...
- php获得网站根目录的几个方法
php获得网站根目录的几个方法 电脑软硬件应用网 45IT.COM 时间:2015-01-08 12:54 作者:佚名 在php中我们要得到网站根目录可以用很多全局变量实现了,如可以利用__file_ ...
- [转]理解OAuth 2.0
作者: 阮一峰 OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版. 本文对OAuth 2.0的设计思路和运行流程,做一个简明通俗的解释, ...
- BundleConfig 的使用 通配符
//是不是说一定要是前缀文件呢 OK 通过 通配符只能使用在前缀或后缀 //捆绑名称 bundles.Add(new StyleBundle("~/caijinhao/caijinhao&q ...
- nginx反向代理初探
1.安装nginx 2.在nginx.conf的http区段中配置负载均衡段 #cluserupstream myCluster{ server 192.168.1.110:1300 weight=5 ...
- java Semaphore
//Listing 6-4. Using a Counting Semaphore to Control Access to a Pool of Items import java.util.conc ...
- SQL代码创建表例子
create table class ( code ) primary Key, name ) ); create table student ( code ) primary Key, name ) ...
- Solr4.3之检索建议suggest
原文链接:http://www.656463.com/article/Efm26v.htm 很多才学solr的人,都容易把solr spellcheck和solr suggest混淆,误以为他们是一样 ...