先上几个小图,看看效果
源码
设置根视图
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window makeKeyAndVisible];
ViewController *viewController = [[ViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = navigationController;
return YES;
}
一级视图
#import
@interface ViewController : UITableViewController
@end
#import "ViewController.h"
#import "CitiesViewController.h"
@interface ViewController ()
//用于获取plist数据
@property (strong, nonatomic) NSDictionary *dictData;
//此Table中展示的数据
@property (nonatomic, strong) NSArray *listData;
@end
@implementation ViewController
//懒加载数据
- (NSDictionary *)dictData{
if (!_dictData) {
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle pathForResource:@"provinces_cities" ofType:@"plist"];
_dictData = [[NSDictionary alloc] initWithContentsOfFile:path];
}
return _dictData;
}
//获取数据字典中的所有键值作为此视图展示的数据
- (NSArray *)listData{
if (!_listData) {
_listData = [self.dictData allKeys];
}
return _listData;
}
- (void)viewDidLoad {
[super viewDidLoad];
// UITableViewController遵守了两个相关协议,此处可以省略
// self.tableView.delegate = self;
// self.tableView.dataSource = self;
self.title = @"省份信息";
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark --实现表视图数据源方法
//此demo中只有一组,返回总行数就可以,如果分组,则要同时考虑组和每组行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.listData count];
}
//cell,为单元格提供数据,必须实现
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSInteger row = [indexPath row];
cell.textLabel.text = self.listData[row];
//cell的扩展类型为扩展指示器,触摸该图标切换到下级视图
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
#pragma mark - 实现表视图委托方法
//响应选择单元格时调用的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSInteger selectedIndex = [indexPath row];
//表样式为普通表视图
CitiesViewController *citiesViewController = [[CitiesViewController alloc] initWithStyle:UITableViewStylePlain];
NSString *selectedName = self.listData[selectedIndex];
citiesViewController.title = selectedName;
citiesViewController.listData = self.dictData[selectedName];
[self.navigationController pushViewController:citiesViewController animated:true];
}
@end
二级视图
#import
@interface CitiesViewController : UITableViewController
@property (nonatomic, weak) NSArray *listData;
@end
#import "CitiesViewController.h"
#import "DetailViewController.h"
@interface CitiesViewController ()
@end
@implementation CitiesViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark --实现表视图数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
NSInteger row = [indexPath row];
NSDictionary *dict = self.listData[row];
cell.textLabel.text = dict[@"name"];
//细节展示按钮
cell.accessoryType = UITableViewCellAccessoryDetailButton;
return cell;
}
#pragma mark --实现表示图委托协议方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSInteger selectedIndex = [indexPath row];
NSDictionary *dict = self.listData[selectedIndex];
DetailViewController *detailViewController = [[DetailViewController alloc] init];
detailViewController.title = dict[@"name"];
detailViewController.url = dict[@"url"];
[self.navigationController pushViewController:detailViewController animated:true];
}
@end
三级视图
#import
@interface DetailViewController : UIViewController
@property (weak, nonatomic) NSString *url;
@end
#import "DetailViewController.h"
#import "WebKit/WebKit.h"
@interface DetailViewController ()
@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, strong) UIActivityIndicatorView *activityIndicatorView;
@end
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.webView = [[WKWebView alloc] initWithFrame:self.view.frame];
[self.view addSubview:self.webView];
self.webView.navigationDelegate = self;
NSURL *url = [NSURL URLWithString:self.url];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
[self addActivityIndicatorView];
}
//添加一个活动指示器,网页未载入时,给用户反馈,也可以使用开源的如MBProgressHUB
- (void)addActivityIndicatorView{
self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
CGRect activityIndicatorViewFrame = self.activityIndicatorView.frame;
CGRect screen = [[UIScreen mainScreen] bounds];
activityIndicatorViewFrame.origin = CGPointMake((screen.size.width-activityIndicatorViewFrame.size.width)/2,200);
//为了是指示器看的清楚,加个背景
self.activityIndicatorView.backgroundColor = [UIColor grayColor];
self.activityIndicatorView.frame = activityIndicatorViewFrame;
self.activityIndicatorView.hidesWhenStopped = true;
[self.view addSubview:self.activityIndicatorView];
// [self.activityIndicatorView startAnimating];
}
//在二级过渡到三级视图时,活动指示器出现的感觉有点早,不是很完美,试图在控制器的生命周期上解决,但还是感觉不够完美
- (void)viewWillAppear:(BOOL)animated{
NSLog(@"View 即将出现");
// [self.activityIndicatorView startAnimating];
}
- (void)viewDidAppear:(BOOL)animated{
NSLog(@"View 出现");
[self.activityIndicatorView startAnimating];
}
- (void)viewDidDisappear:(BOOL)animated{
NSLog(@"View 消失");
}
- (void)dealloc{
NSLog(@"死亡");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark --实现WKNavigationDelegate委托方法
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
NSLog(@"开始加载");
}
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
NSLog(@"内容开始返回");
[self.activityIndicatorView stopAnimating];
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
NSLog(@"加载完成");
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error{
NSLog(@"加载失败 error : %@", error.description);
}
@end
** 调试图**
** plist 文件**