码迷,mamicode.com
首页 > 其他好文 > 详细

基于树形结构的导航实现

时间:2015-08-17 21:26:45      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

记得给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

 

基于树形结构的导航实现

标签:

原文地址:http://www.cnblogs.com/lhb-gn8080/p/4737680.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!