一个TableView实现省市区字典数组的跳转

AppDelegate.m

#import "AppDelegate.h"
#import "MainViewController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate
-(void)dealloc
{
    [_window release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    [_window release];




    MainViewController *mainVC=[[MainViewController alloc]init];
    mainVC.num = 1;



    NSString *path=@"/Users/dllo/Downloads/UI08_tableview省市区字典数组副本/UI08_tableview省市区字典数组/area(1).txt";

    NSString *str=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *strArr=[str componentsSeparatedByString:@"\n"];

    mainVC.proArr=[NSMutableArray array];

    for (NSString *temp in strArr) {
        if (![temp hasPrefix:@" "]) {
            // 创建一个省字典
            NSMutableDictionary *proDic = [NSMutableDictionary dictionary];
            // 给省字典添加键值对
            // 省名
            [proDic setObject:temp forKey:@"proName"];
            // 创建一个市数组
            NSMutableArray *cityArr = [NSMutableArray array];
            // 添加到省字典里
            [proDic setObject:cityArr forKey:@"cityArr"];
            // 把省字典添加到省数组里
            [mainVC.proArr addObject:proDic];
        } else if ([temp hasPrefix:@"  "] && ![temp hasPrefix:@"    "]){
            // 创建一个市字典
            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
            // 给市字典添加键值对
            // 市名
            [cityDic setObject:temp forKey:@"cityName"];
            // 创建一个区数组
            NSMutableArray *zoneArr = [NSMutableArray array];
            // 添加到市字典里
            [cityDic setObject:zoneArr forKey:@"zoneArr"];
            // 给市字典找一个位置
            // 先找到对应的省字典
            NSMutableDictionary *proDic = [mainVC.proArr lastObject];
            // 再找到对应的市数组
            NSMutableArray *cityArr = proDic[@"cityArr"];
            // 将市字典添加到市数组里
            [cityArr addObject:cityDic];
        } else {
            // 先找省字典
            NSMutableDictionary *proDic = [mainVC.proArr lastObject];
            // 市数组
            NSMutableArray *cityArr = proDic[@"cityArr"];
            // 市字典
            NSMutableDictionary *cityDic = [cityArr lastObject];
            // 区数组
            NSMutableArray *zoneArr = cityDic[@"zoneArr"];
            // 将区名添加到区数组里
            [zoneArr addObject:temp];
        }
    }



    UINavigationController *naVC=[[UINavigationController alloc]initWithRootViewController:mainVC];
    self.window.rootViewController=naVC;
    [mainVC release];
    [naVC release];
    return YES;
}

MainViewController.h

#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController

@property(nonatomic,retain)NSMutableArray *proArr;
@property(nonatomic,assign)int num;

@end

MainViewController.m

#import "MainViewController.h"

@interface MainViewController ()<UITableViewDataSource,UITableViewDelegate>

@end

@implementation MainViewController


- (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor=[UIColor yellowColor];
    self.navigationController.navigationBar.translucent=NO;


    UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-64) style:UITableViewStylePlain];
    [self.view addSubview:tableView];
    tableView.dataSource=self;
    tableView.delegate=self;
    [tableView release];


}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.proArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse=@"reuse";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
    }

    if (self.num == 1) {
        NSMutableDictionary *proDic=self.proArr[indexPath.row];

        cell.textLabel.text=proDic[@"proName"];
    }

    if (self.num == 2) {

        NSMutableArray *arr= self.proArr;
        NSMutableDictionary *cityDic=arr[indexPath.row];
        cell.textLabel.text=cityDic[@"cityName"];
    }

    if (self.num == 3) {
        cell.textLabel.text=self.proArr[indexPath.row];
    }
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (self.num == 1) {
        MainViewController *secVC=[[MainViewController alloc]init];
        [self.navigationController pushViewController:secVC animated:YES];
        secVC.proArr = self.proArr[indexPath.row][@"cityArr"];
        [secVC release];
        secVC.num = self.num + 1;
    }
    if (self.num == 2) {
        MainViewController *secVC=[[MainViewController alloc]init];
        [self.navigationController pushViewController:secVC animated:YES];
        secVC.proArr = self.proArr[indexPath.row][@"zoneArr"];
        [secVC release];
        secVC.num = self.num + 1;

    }

    if (self.num == 3) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值