重点内容
1. 动态添加cell.
2. 创建WebView调用javaScript代码,实现点击某个cell,跳转到指定的段落.
3. 用webView加载html文件.
4. 调用loadView方法,将系统的view换成WebView.
动态创建Cell 与加载WebView (调用javaScript代码)
控制器(C)
重点分析:
(1). 解析json文件.
(2). 动态设置cell,遍历存储有模型的数组,调用添加标题,图片和跳转的类的方法,添加标题,图片,跳转的类,添加到可变数组中,再将可变数组传入组(模型)中,最后添加到dataArray数组中.
(3). 点击cell时,传递每个cell的数据,给显示的网页设置不同的数据模型.
#import <UIKit/UIKit.h>
#import "BaseTableViewController.h"
/**1.继承包含设置所有cell共有方法的类*/
@interface HelpViewController : BaseTableViewController
@end
解析json数据,动态添加cell,点击cell配置不同的数据模型
#import "HelpViewController.h"
#import "SettingHtmlModel.h"
#import "SettingArrowModel.h"
#import "Group.h"
#import "HtmlViewController.h"
#import "myNavViewController.h"
@interface HelpViewController ()
@property (strong, nonatomic) NSMutableArray *htmls;
@end
@implementation HelpViewController
- (NSMutableArray *)htmls{
if (_htmls == nil){
/**1.读取json文件*/
NSString *path = [[NSBundle mainBundle] pathForResource:@"help.json" ofType:nil];
/**2.转换成data*/
NSData *data = [NSData dataWithContentsOfFile:path];
/**3.换json格式文件解析成数组*/
NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
/**4.创建数据模型*/
NSMutableArray *dataArray = [NSMutableArray array];
for (NSDictionary *dic in array) {
SettingHtmlModel *htmls = [SettingHtmlModel settingHemlModelWithDictionary:dic];
[dataArray addObject:htmls];
}
_htmls = dataArray;
}
return _htmls;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
/**1.创建一个可变数组存储所有cell的模型*/
NSMutableArray *items = [NSMutableArray array];
/**2.遍历数组中所有的数据模型*/
for (SettingHtmlModel *htmls in self.htmls){
/**3.设置cell的title*/
SettingModel *item = [SettingArrowModel settingModelWithTitle:htmls.title andIcon:nil andDesClass:nil];
/**4.添加到数组中*/
[items addObject:item];
}
/**5.创建组*/
Group *group = [[Group alloc] init];
group.items = items;
[self.dataArray addObject:group];
}
/**点击cell跳转到不同页面*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
HtmlViewController *htmlVC = [[HtmlViewController alloc] init];
htmlVC.htmls = self.htmls[indexPath.row];
myNavViewController *nav = [[myNavViewController alloc] initWithRootViewController:htmlVC];
[self presentViewController:nav animated:YES completion:nil];
}
@end
二. 数据模型 (M)
#import <Foundation/Foundation.h>
@interface SettingHtmlModel : NSObject
/**标题*/
@property (copy, nonatomic) NSString *title;
/**ID*/
@property (copy, nonatomic) NSString *ID;
/**网址*/
@property (copy, nonatomic) NSString *html;
- (instancetype)initWithDictionary:(NSDictionary *)dic;
+ (instancetype)settingHtmlModelWithDictionary:(NSDictionary *)dic;
@end
/**.m文件,实现方法*/
#import "SettingHtmlModel.h"
@implementation SettingHtmlModel
+ (instancetype)settingHtmlModelWithDictionary:(NSDictionary *)dic{
return [[self alloc] initWithDictionary:dic];
}
- (instancetype)initWithDictionary:(NSDictionary *)dic{
if (self = [super init]){
[self setValuesForKeysWithDictionary:dic];
self.ID = dic[@"id"];
}
return self;
}
/**当找不到的话,不会崩*/
- (void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
@end
三. WebView 视图(V)
#import <UIKit/UIKit.h>
@class SettingHtmlModel;
@interface HtmlViewController : UIViewController
@property (strong, nonatomic) SettingHtmlModel *htmls;
@end
#import "HtmlViewController.h"
#import "SettingHtmlModel.h"
@interface HtmlViewController ()<UIWebViewDelegate>
@end
@implementation HtmlViewController
调用loadview把view换成WebView
- (void)loadView{
self.view = [[UIWebView alloc] init];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
/**1.设置标题*/
self.title = self.htmls.title;
/**2.设置WebView*/
UIWebView *webView = (UIWebView *)self.view;
/**3.设置代理*/
webView.delegate = self;
/**4.创建URL*/
NSURL *url = [[NSBundle mainBundle] URLForResource:self.htmls.html withExtension:nil];
/**5.创建请求*/
NSURLRequest *request = [NSURLRequest requestWithURL:url];
/**6.发送请求加载网页*/
[webView loadRequest:request];
/**7.设置左上角关闭按钮*/
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:self action:@selector(closes)];
}
/**关闭事件*/
- (void)closes{
[self dismissViewControllerAnimated:YES completion:nil];
}
加载javaScript代码,实现点击某个cell,跳转到指定的段落(共用一页)
#pragma mark--网页加载完毕后调用
- (void)webViewDidFinishLoad:(UIWebView *)webView{
/**跳到id对应的网页标签,'#%@'; javescript一般是用单引号来括起来的,也能用""号,并且用分号结束*/
/**1.拼接JavaCript代码*/
NSString *js = [NSString stringWithFormat:@"window.location.href = '#%@';", self.htmls.ID];
/**2.执行javaCript代码*/
[webView stringByEvaluatingJavaScriptFromString:js];
}
@end