IOS TableView实现省市联动

之前用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=0;

    _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[email protected]{@"广东省":@[@"深圳",@"广州",@"东莞"],@"河南省":@[@"驻马店",@"周口",@"郑州"]};

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_firstTable]-0-[_secondTable(_firstTable)]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_firstTable,_secondTable)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-80-[_firstTable]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_firstTable)]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-80-[_secondTable]-0-|" options:0 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;
    }
    return 0;
}

- (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

时间: 2024-07-29 05:33:21

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

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

两个 TableView 之间的联动, TableView 与 CollectionView 之间的联动 这是一个创建于 359 天前的主题,其中的信息可能已经有所发展或是发生改变. [联动] :两个 TableView 之间的联动, TableView 与 CollectionView 之间的联动 前言 现在市面上有很多 app 都有联动功能,有的是两个 TableView 之间的联动,比如美团外卖,百度外卖,饿了么等等.有的是 TableView 与 CollectionView 之间的联动

自建List&lt;&gt;绑定ComboBox下拉框实现省市联动

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace _04省市联动 { public partial cl

php省市联动实现

设计模式:ajax实现,数据库格式:id,name,parent_id 数据库: CREATE TABLE IF NOT EXISTS `city` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(30) DEFAULT NULL, `parent_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREME

fragment 中利用spinner实现省市联动

(1)布局文件就不在说明了,主要说代码的实现,先把代码贴上! package com.example.cl; import android.annotation.SuppressLint; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import

省市联动 js

工作中见到这个省市联动代码,虽然很简单也能写出来,还是随便把它记录下来. //省市联动 function area(obj_id, area_pId, data_call_back) { if (area_pId == -1) return; $.ajax({ type: 'GET', url: "/SysAdmin/Pages/tb_supplierAdd.aspx", data: { area_pId: area_pId }, dataType: 'json', success:

jquery插件-省市联动

由于项目需要需要实现一个省市联动,由于业务有一些特殊的需求,使用现有的插件略有不便,就自己实现了一个. 首先需要保存地区数据的JS数据文件,我这里命名为areaData.js,内容如下: /** * 保存地区信息 * 数据格式 * areaData = [{'pro': '北京', 'cities': {'-1': '北京'}}, {...}] * 直辖市存在-1,表示就是直辖市 */ (function(window) { window.areaData = [{"pro":&quo

iOS tableview

每个section的row数量(都是从0下标开始) (http://blog.csdn.net/hmt20130412/article/details/20831377) iOS tableview,布布扣,bubuko.com

几个数据库的小案例(二):极其简单的省市联动

总用有两个文件(frmMain.cs SqlHelper.cs) //frmMain.cs//作者:Meusing System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace 省市联动

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

我们经常要实现类似省市联动一样的功能,常见的就是二个查找控件,一个选择了省后,另一个市的查找控件就自动过滤了,只显示当前省下的市,而不是所有的市.当然这是最简单的,实际工作中还有更复杂的功能要通过过滤查找控件来实现.本文主要介绍基本的查找控件过滤.多表关联的复杂过滤以及子表里实现查找控件的过滤.   一.简单的过滤 先看下需求: 按"Special GL Indicator" 来过滤 Posting 查找控件增加了preSearch事件.它发生在查找控件显示对话框供用户查找记录之前,与