UI 08 tableView版中国省市区 -- 3页

还记得之前写的中国省市区么?

现在我们使用tableView将他显示出来.

里面用到了从前向后属性传值.

第一页效果图如下, 一共31个省

#import "ProViewController.h"
#import "CityViewController.h"
@interface ProViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, retain)NSMutableArray *proArr;

@end

@implementation ProViewController
- (void)dealloc{
    [_proArr release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self createData];
    }
    return self;
}

- (void)createData{

    NSString *path = @"/Users/dllo/Desktop/UI 学习/UI08TableView 省市区./UI08TableView 省市区./area.txt";
    NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *strArr = [str componentsSeparatedByString:@"\n"];
    self.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"];

            [self.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 = [self.proArr lastObject];
            NSMutableArray *cityarr = prodic[@"cityarr"];
            [cityarr addObject:citydic];
        }else{
            NSMutableDictionary *prodic = [self.proArr lastObject];
            NSMutableArray *cityarr = prodic[@"cityarr"];
            NSMutableDictionary *citydic  = [cityarr lastObject];
            NSMutableArray *zonearr = citydic[@"zonearr"];
            [zonearr addObject:temp];
        }
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor redColor];
    self.title = @"中国省名";

    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 release];
    tableView.delegate = self;
    tableView.dataSource = self;

//    // 读出plist文件内容
//    NSString *path = [[NSBundle mainBundle]pathForResource:@"Student" ofType:@"plist"];
//    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithContentsOfFile:path];
//    NSLog(@"%@",dic);

}
- (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] autorelease];
    }
    NSMutableDictionary *prodic = self.proArr[indexPath.row];
    cell.textLabel.text = prodic[@"proname"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CityViewController *cityVC = [[CityViewController alloc] init];
    [self.navigationController pushViewController:cityVC animated:YES];
    [cityVC release];
    // 省字典
    NSMutableDictionary *prodic = self.proArr[indexPath.row];
    //省对应的市数组
   cityVC.cityarr = prodic[@"cityarr"];

}

第二页:

#import "CityViewController.h"
#import "ZoomViewController.h"
@interface CityViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation CityViewController
- (void)dealloc{
    [_cityarr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blueColor];
   // NSLog(@"%@",self.cityarr);
     self.title = @"市名";
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStyleGrouped];
    [self.view addSubview:tableView];
    [tableView release];

    tableView.delegate = self;
    tableView.dataSource = self;

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.cityarr.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];
    }
    NSMutableDictionary *citydic = self.cityarr[indexPath.row];
    cell.textLabel.text = citydic[@"cityname"];

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZoomViewController *zoneVC = [[ZoomViewController alloc] init];
    [self.navigationController pushViewController:zoneVC animated:YES];
    [zoneVC release];

    NSMutableDictionary *citydic = self.cityarr[indexPath.row];
    zoneVC.zonearr = citydic[@"zonearr"];

}

第三页:

#import "ZoomViewController.h"

@interface ZoomViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
@property(nonatomic, retain)UIAlertView *alet;

@end

@implementation ZoomViewController
- (void)dealloc{
    [_zonearr release];
    [_alet release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     self.title = @"区名";
    self.view.backgroundColor = [UIColor greenColor];
    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 release];

    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.rowHeight = 100;
    self.alet = [[UIAlertView alloc] initWithTitle:@"要返回到市名吗?" message:nil delegate:self cancelButtonTitle:@"返回市名" otherButtonTitles:@"返回主页",@"Cancel", nil];

}
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.zonearr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuse] autorelease];
    }
    cell.textLabel.text = self.zonearr[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [self.alet show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        [self.navigationController popViewControllerAnimated:YES];
    }else if (buttonIndex == 1){
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
}

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

时间: 2024-08-03 18:34:27

UI 08 tableView版中国省市区 -- 3页的相关文章

中国省市区3级数据表(mysql)

中国省市区3级数据表 mysql脚本见附件,TXT改为sql

中国省市区地址三级联动jQuery插件 案例下载

中国省市区地址三级联动jQuery插件 案例下载 distpicker 是一款可以实现中国省市区地址三级联动jQuery插件.它使用简单,简单设置即可完成中国省市区地址联动效果. 安装 可以通过npm或bower来安装该三级联动插件. npm install distpicker bower install distpicker 使用方法 HTML结构 基本的HTML结构是使用一个<div>容器来包裹一组<select>元素. <div><!-- containe

Android scrollview 上滑固定某一控件(美团团购详情UI)完美版

在scrollview 上滑固定某一控件(美团团购详情UI)文中介绍了怎么用touchlistener实现类似上滑停住的效果,但是这种方法存在一个明显的bug,就是在内容比较多的时候, 大部分人都是以滑动方式查看内容,而不是touch的方式,这就会导致最上面的滑块出现不及时,或者延后的现象,这里介绍一个全新的方法去实现类似效果,可以很好的解决以上问题. 目前在scrollview中没有onscrolllistener所以需要自己去实现,先复写一个scrollview: package com.e

UI 09 tableView 中国省市区. 一个页面, 三个tableView

感觉省市区要被玩坏了---. #import "MainViewController.h" #define WIDTH self.view.frame.size.width #define HEIGHT self.view.frame.size.height @interface MainViewController ()<UITableViewDataSource, UITableViewDelegate> @property (nonatomic,retain)NSMu

基于Element UI Cascader 级联选择器的中国省市区级联数据

安装 npm install element-china-area-data -save 在线演示:https://plortinus.github.io/element-china-area-data/index.html github:https://github.com/airyland/china-area-data 引入 import { provinceAndCityData, regionData, provinceAndCityDataPlus, regionDataPlus, 

iOS开发-UI (八)TableView

知识点: 1.UITableView使用 2.UITableView分段功能 3.UITableViewCell重用机制 ======================= UITableView使用 1.UITableView作用 2.UITableView创建 - (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style; UITableViewStyle: UITableViewStylePlain       列表模式 UIT

PHP + jQuery + Json 实现中国省市区三级联动

定义省市区结构的JSON文件代码: city.data.js: var cityData = [{ value: '110000', text: '北京市', children: [{ value: "110100", text: "北京市", children: [{ value: "110101", text: "东城区" }, { value: "110102", text: "西城区&qu

动态构建TreeView(中国省市区)

.aspx代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="中国市区县.aspx.cs" Inherits="中国市区县" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtm

红色版中国菜刀(20141213)正式发布 过狗红色菜刀

下载请认准官方网站:http://www.maicaidao.com/ 不过现在官方已经打不开了,很多朋友找不到下载地址,所以现在放出来,有需要的朋友可以自行下载! 下载地址:https://www.lanzous.com/i1xg3ib 软件简介: 软件名称:中国菜刀(China chopper)官方网站:http://www.maicaidao.com/------------------------------------免责申明:请使用者注意使用环境并遵守国家相关法律法规!由于使用不当造