之前用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. 异步和等待(async和await)

    在.Net 4.5中,通过async和await两个关键字,引入了一种新的基于任务的异步编程模型(TAP).在这种方式下,可以通过类似同步方式编写异步代码,极大简化了异步编程模型.如下式一个简单的实例 ...

  2. UWP开发入门(三)——{x:Bind}扩展标记

    上周打炉石打得太晚……忘记更新了,本周补上.本篇我们讲一下{x:Bind}扩展标记.{x:Bind}扩展标记也是Windows 10 Uinversal 新增的内容,按官方的说法是 {Binding} ...

  3. https经过ngxin在web服务器收到http请求

    一. 问题描述: nginx代理nexus的时候页面不全,部分https地址自动转成http了,使用方案四解决的 浏览器输入https://www.mysite.com,后台通过request.get ...

  4. LOJ#6047. 「雅礼集训 2017 Day10」决斗(set)

    题面 传送门 题解 这么简单一道题我考试的时候居然只打了\(40\)分暴力? 如果我们把每个点的\(a_i\)记为\(deg_i-1\),其中\(deg_i\)表示有\(deg_i\)个数的\(A_i ...

  5. 【微信小程序】——rpx、px、rem等尺寸间关系浅析

    最近开发微信小程序,在写样式表的时候发现用PX的效果不太理想,而官方文档用rpx来做响应式布局单位,就仔细研究了下,在此做个小总结: 这里先引用官方定义的尺寸单位‘rpx’:可以根据屏幕宽度进行自适应 ...

  6. [真题] 一道 vsftp 运维题

    一道 vsftp 运维题 一.前言 在 V 站上凑巧看到了好友发的求助帖,五天时间一个理他的都没有.哈哈哈~ 废话不多说,我们来试试. 二.题目 这里我们假设存在这样的场景: 网络内有普通用户 ade ...

  7. 【学习小记】KD-Tree

    Preface 听说KD树实在是个大神器 可以解决多维空间多维偏序点权和,可以求某个点的空间最近最远点 就二维平面上的来说,复杂度在\(O(n\log n)\)到\(O(n\sqrt n)\)不等 嫌 ...

  8. 【App性能】:TraceView分析法

    抓取traceview的日志有两种方式, 1,是在代码中片段中添加: Debug.startMethodTracing(“hello”); ....... Debug.stopMethodTracin ...

  9. [性能测试]:ISO8583报文解析实例

    现在我们有ISO8583报文如下(十六进制表示法): 60 00 03 00 00 60 31 00 31 07 30 02 00 30 20 04 C0 20 C0 98 11 00 00 00 0 ...

  10. 认识python正则模块re

    python正则模块re python中re中内置匹配.搜索.替换方法见博客---python附录-re.py模块源码(含re官方文档链接) 正则的应用是处理一些字符串,phthon的博文python ...