之前用UIPickerView实现了省市联动,上个月网友让用UITableView给他实现了下。今天也把这些贴出来。

//
//  ViewController.m
//  doubleTable
//
//  Created by City--Online on 15/8/5.
//  Copyright (c) 2015年 City--Online. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong) NSDictionary *dicData;

//第一个tableView选择的Index
@property (nonatomic,assign) int *firstIndex;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _firstIndex=;

    _firstTable.delegate=self;
    _firstTable.dataSource=self;
    _firstTable.translatesAutoresizingMaskIntoConstraints = NO;
    _firstTable.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];

    _secondTable.delegate=self;
    _secondTable.dataSource=self;
    _secondTable.translatesAutoresizingMaskIntoConstraints = NO;
    _secondTable.tableFooterView=[[UIView alloc]initWithFrame:CGRectZero];

    _dicData=@{@"广东省":@[@"深圳",@"广州",@"东莞"],@"河南省":@[@"驻马店",@"周口",@"郑州"]};

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: metrics:nil views:NSDictionaryOfVariableBindings(_firstTable,_secondTable)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: metrics:nil views:NSDictionaryOfVariableBindings(_firstTable)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: metrics:nil views:NSDictionaryOfVariableBindings(_secondTable)]];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView==_firstTable) {
        return _dicData.allKeys.count;
    }
    else if (tableView==_secondTable)
    {
        NSString *key=[_dicData.allKeys objectAtIndex:_firstIndex];
        NSArray *arr=[_dicData objectForKey:key];
        return arr.count;
    }
    ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }

    if (tableView==_firstTable) {
        if (indexPath.row==_firstIndex) {
            cell.backgroundColor=[UIColor redColor];
        }
        else
        {
            cell.backgroundColor=[UIColor whiteColor];
        }
         cell.textLabel.text=[_dicData.allKeys objectAtIndex:indexPath.row];
    }
    else
    {
        cell.backgroundColor=[UIColor grayColor];
        NSString *key=[_dicData.allKeys objectAtIndex:_firstIndex];
        NSArray *arr=[_dicData objectForKey:key];
        cell.textLabel.text=[arr objectAtIndex:indexPath.row];
    }

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView==_firstTable) {
        _firstIndex=indexPath.row;
        [_secondTable reloadData];
        [_firstTable reloadData];
    }
    else if (tableView==_secondTable)
    {
        NSLog(@"%ld  %ld",_firstIndex,indexPath.row);
    }
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }

#ifdef __IPHONE_8_0
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }

    if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
#endif
}

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

@end

IOS TableView实现省市联动的更多相关文章

  1. ios 两个 TableView 之间的联动, TableView 与 CollectionView 之间的联动

    两个 TableView 之间的联动, TableView 与 CollectionView 之间的联动 这是一个创建于 359 天前的主题,其中的信息可能已经有所发展或是发生改变. [联动] :两个 ...

  2. jquery省市联动,根据公司需求而写

    //author:guan //2015-05-25 //省市联动 //实用说明,页面引用如下js //<script src="../js/jquery-1.6.3.min.js&q ...

  3. Json 基于jQuery+JSON的省市联动效果

    helloweba.com 作者:月光光 时间:2012-09-12 21:57 标签: jQuery  JSON  Ajax  省市联动     省市区联动下拉效果在WEB中应用非常广泛,尤其在一些 ...

  4. AJAX案例四:省市联动

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  5. Dynamic CRM 2013学习笔记(八)过滤查找控件 (类似省市联动)

    我们经常要实现类似省市联动一样的功能,常见的就是二个查找控件,一个选择了省后,另一个市的查找控件就自动过滤了,只显示当前省下的市,而不是所有的市.当然这是最简单的,实际工作中还有更复杂的功能要通过过滤 ...

  6. [JS]以下是JS省市联动菜单代码

    以下是JS省市联动菜单代码: 代码一: <html> <head> <title></title> <script language=" ...

  7. 通过Javascript数组设计一个省市联动菜单

    通过Javascript数组设计一个省市联动菜单 使用Option内置类来完成下拉选项的创建 2.使用定时器实现一个时钟程序 3.使用PHP+JSON完成语音验证码 网址:http://yuyin.b ...

  8. Ado.Net小练习03(省市联动)

    前台界面:          后台代码: namespace _04省市联动 {     public partial class Form1 : Form     {         public ...

  9. Ajax省市联动

    以JQuery为JS,写的Ajax省市联动. 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&q ...

随机推荐

  1. ADO.NET操作SQL Server:数据库操作类(未封装)

    1.添加数据 /// <summary> /// 添加数据 /// </summary> /// <param name="newEntity"> ...

  2. cesium编程入门(四)界面介绍及小控件隐藏

    感性认识 界面介绍,viewer Geocoder : 查找位置工具,查找到之后会将镜头对准找到的地址,默认使用bing地图 Home Button :视角返回初始位置. Scene Mode Pic ...

  3. 安卓 往SD卡里写文件不能及时更新的问题

    我们做Android开发时奖保存图片到SD卡,但是Gallery中不能及时显示 下面我找到了问题所在然后解决了这个问题. 当保存图片后打开gallery时,有的会自动给你刷新,有的不行,这样就导致图片 ...

  4. PHP set_error_handler()函数的使用

    我们写程序,难免会有问题(是经常会遇到问题 ),而PHP遇到错误时,就会给出出错脚本的位置.行数和原因.有很多人说,这并没有什么大不了.确实,在调试程序阶段,这确实是没啥的,而且我认为给出错误路径是必 ...

  5. [Scala随用随学] —— sealed声明的作用

    今天在看MLlib的源码时,看到Vector的声明是sealed trait,很好奇这个sealed有什么作用,与是搜到了这个文章: http://www.cnblogs.com/rollenholt ...

  6. javascript获取网址参数

    通过以上图片,就可以很好的理解: location.href location.protocol location.host location.hostname location.port locat ...

  7. JAVA数组的定义方式

    JAVA中,数组有以下几种定义方式: 1.先定义数组中元素的个数,然后再进行元素赋值 int[] a=new int[2]; a[0]='a'; a[1]='b';a[2]='c'; 2.元素个数与元 ...

  8. 浅谈Android选项卡(三)

    上一节介绍了TabActivity的简单用法,但是现在的Api中已经不建议使用了,建议使用Fragment来替代以上的功能,下面介绍下使用Fragment和ViewPager的结合使用. http:/ ...

  9. spring-boot-maven-plugin多模块install问题解决办法

    一.问题描述: 项目分多个模块,open-eureka注册中心.open-provider服务提供者.open-common公共部分,provider依赖common.父pom使用spring-boo ...

  10. python 类中的某个函数作为装饰器

    在python的类中,制作一个装饰器的函数, class A: def wrapper(func): ###装饰器 def wrapped(self,*arg,**kwargs) ... return ...