UI_UITableView省市区字典数组

原创 2015年09月30日 21:07:26

这里写图片描述

RootViewController.m
#import "RootViewController.h"
#import "SecondViewController.h"

@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *provinceArray;
@end

@implementation RootViewController

- (void)dealloc
{
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.tableView];
    [_tableView release];

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 100;
    [self createData];
}

- (void)createData {
    NSString *path = @"/Users/dlios/Desktop/UI09_UITableView省市区字典数组/UI09_UITableView省市区字典数组/area(省市区版).txt";
    NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *strArr = [str componentsSeparatedByString:@"\n"];
    self.provinceArray = [NSMutableArray array];
    for (NSString *tempStr in strArr) {
        if (![tempStr hasPrefix:@" "]) {
            NSMutableDictionary *provinceDictionary = [NSMutableDictionary dictionary];
            [provinceDictionary setObject:tempStr forKey:@"provinceName"];
            NSMutableArray *cityArray = [NSMutableArray array];
            [provinceDictionary setObject:cityArray forKey:@"cityArray"];
            [self.provinceArray addObject:provinceDictionary];

        } else if ([tempStr hasPrefix:@"  "] && ![tempStr hasPrefix:@"    "]) {
            NSMutableDictionary *cityDictionary = [NSMutableDictionary dictionary];
            [cityDictionary setObject:tempStr forKey:@"cityName"];
            NSMutableArray *zoneArray = [NSMutableArray array];
            [cityDictionary setObject:zoneArray forKey:@"zoneArray"];
            NSMutableDictionary *provinecDictionary = [self.provinceArray lastObject];
            NSMutableArray *cityArray = provinecDictionary[@"cityArray"];
            [cityArray addObject:cityDictionary];
        } else {
            NSMutableDictionary *provinceDictionary = [self.provinceArray lastObject];
            NSMutableArray *cityArray = provinceDictionary[@"cityArray"];
            NSMutableDictionary *cityDictionary = cityArray.lastObject;
            NSMutableArray *zoneArray = cityDictionary[@"zoneArray"];
            [zoneArray addObject:tempStr];
        }
    }
}


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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    cell.textLabel.text = self.provinceArray[indexPath.row][@"provinceName"];
    cell.detailTextLabel.text = @"2";
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:secondVC animated:YES];
    [secondVC release];

    // 属性传值.
    secondVC.arr = self.provinceArray[indexPath.row][@"cityArray"];
}
SecondViewController.h
#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@property(nonatomic, retain)NSMutableArray *arr;
@end
SecondViewController.m

#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface SecondViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, retain)UITableView *tableView;

@end

@implementation SecondViewController

- (void)dealloc
{
    [_arr release];
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

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

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 100;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
    cell.textLabel.text = self.arr[indexPath.row][@"cityName"];
    cell.detailTextLabel.text = @"2";
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
    [self.navigationController pushViewController:thirdVC animated:YES];
    [thirdVC release];

    thirdVC.arr1 = self.arr[indexPath.row][@"zoneArray"];
}
ThirdViewController.h
#import <UIKit/UIKit.h>

@interface ThirdViewController : UIViewController

@property(nonatomic, retain)NSMutableArray *arr1;

@end
ThirdViewController.m
#import "ThirdViewController.h"

@interface ThirdViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, retain)UITableView *tableView;

@end

@implementation ThirdViewController

- (void)dealloc
{
    [_arr1 release];
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

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

    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 100;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse] autorelease];
    }

    cell.textLabel.text = self.arr1[indexPath.row];
    return cell;
}

这里写图片描述

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

UI09_UITableView省市区字典数组

UITableView, 省市区

UITableView 数组与字典结合(省市区)

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"area" ofType:@"txt"];         NSE...

UI09_多种UITableView(一个界面出现三个tableView,省市区数组)

前期准备(1)定义数组与TableView属性 @property(nonatomic,retain)NSMutableArray *proArr; @property(nonatomic,retai...

带分区的UITableView的创建(省市区字典)

创建MainViewController1.初始化代码-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle ...

UI_三个UITableView的省市区联动

UI_三个UITableView的省市区联动

OC中字典的使用,数组的使用----省市区

在OC学习中,数组和字典的简单使用

objective-c 省市区练习(数组与字典的嵌套)

啥也别说了直接上代码: //找文件位置 有两种方法 NSString *path = @"/Users/lan/Desktop/OC语言/Wzh_OC_07_省市区练习/Wzh_OC_07_省...

#Objective-C 经典字典数组排序 - 省市区

1.首先要准备好包含所有省市区的文档,将文档拖到我们建好的Xcode里 2.然后新建一个路径NSString *path = @" ";   再将省市区文档拖到@"  "中; 3.在进行排序前...

字典+数组的应用——省市区

我们要实现的效果 实现框架图 这是我们的实现代码 NSString *path = @"/Users/dllo/Desktop/省区市/省区市/area.txt"; //文件...

OC用字典数组创建省市区

OC用字典数组创建省市区 //获取文件路径 NSString *path = @"/Users/lan/Desktop/ios开发/百度云同步盘/iOS学习/OC课程/练习/第七天/Creat...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:UI_UITableView省市区字典数组
举报原因:
原因补充:

(最多只允许输入30个字)