基于树形结构的导航实现

记得给segue设定标示符

先设定viewController的Class,然后拉属性,在设置标识符

 

1.LhbTableViewController.h

@interface LhbTableViewController : UITableViewController<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSDictionary *provinceDic;
@property (nonatomic,strong) NSArray *cityArray;
@end

2.LhbTableViewController.m

#import "LhbTableViewController.h"
#import "CityTableViewController.h"
@interface LhbTableViewController ()
@end

@implementation LhbTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"provinces_cities" ofType:@"plist"];
    self.provinceDic = [NSDictionary dictionaryWithContentsOfFile:path];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.provinceDic.allKeys.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];


    //self.provinceDic.allKeys 是字典中所有的key (省份)
    [cell.textLabel setText:[self.provinceDic.allKeys objectAtIndex:indexPath.row]];
    return cell;
}

隐藏状态栏
//-(BOOL)prefersStatusBarHidden
//{
//    return YES;
//}

#pragma mark - Navigation


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    //先比较segue
    if ([segue.identifier isEqualToString:@"province2city"]) {
        //目标控制器
        CityTableViewController *cityVC = segue.destinationViewController;
        //取得省份的名字
        NSString *provinceName = [self.provinceDic.allKeys objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
        
        cityVC.title = provinceName;
        //在各个省份名中取得相应的字典
        cityVC.cities = [self.provinceDic objectForKey:provinceName];
    }
}
@end

3.CityTableViewController.h

@interface CityTableViewController : UITableViewController<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) NSArray *cities;
@end

4.CityTableViewController.m

#import "CityTableViewController.h"
#import "UrlViewController.h"
@interface CityTableViewController ()

@end

@implementation CityTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   }

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cities.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    // 从所在字典中取得key name所对应的value
    [cell.textLabel setText:[[self.cities objectAtIndex:indexPath.row] valueForKey:@"name"]];
    return cell;
}

#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    
    if ([segue.identifier isEqualToString: @"city2url"] ) {
        UrlViewController *urlVC = segue.destinationViewController;
     //[self.tableView indexPathForSelectedRow]  是为了选取对应行
       NSDictionary *cityDic = [self.cities objectAtIndex:[[self.tableView indexPathForSelectedRow] row]];
        
        urlVC.url = [cityDic objectForKey:@"url"];
        urlVC.title = [cityDic objectForKey:@"name"];
    }
}
@end

拉进来一个webView!!!!!

5.UrlViewController.h

@interface UrlViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (nonatomic,strong) NSString *url;
@end

6.UrlViewController.m

#import "UrlViewController.h"
@interface UrlViewController ()
@end


@implementation UrlViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
   //把字符串转化成 NSURL 类型的
    NSURL *url = [NSURL URLWithString:self.url];
    
    NSURLRequest * request =[NSURLRequest requestWithURL:url];
    //加载
    [self.webView loadRequest:request];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end

 

转载于:https://www.cnblogs.com/lhb-gn8080/p/4737680.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值