UITableView---分组 省份城市列表+ 右列索引

UITableView分组显示省份城市列表+右侧索引

例题:分组名为省份名称,组内容为对应省份的城市名称,点击右边的索引,将对应的省份分组显示到到第一行。

显示效果如下:

对应plist文件如下图所示:

#import "ViewController.h"

//遵守协议

@interface
ViewController () <UITableViewDelegate,UITableViewDataSource>

//UITableView

@property (nonatomic,weak)UITableView * tableView;

//数据数组

@property (nonatomic,strong)
NSMutableArray * dataArray;

//cities字典

@property (nonatomic,strong)
NSMutableDictionary * cityDic;

@property (nonatomic,strong)NSMutableArray * cityArray;

//provinces数组

@property (nonatomic,strong)
NSMutableArray * procincesArray;

@property (nonatomic,strong)
NSMutableString * proviNameStr;

//strong ---数据类型

//weak   ---控件之类的,切换页面后若果控件不显示,释放掉

@end

@implementation ViewController

- (void)viewDidLoad

{

[superviewDidLoad];

//加载数据

[self_loadData];

UITableView * table=[[UITableViewalloc]initWithFrame:CGRectMake(0,20,
self.view.frame.size.width,self.view.frame.size.height-20)style:UITableViewStyleGrouped];

table.delegate=self;

table.dataSource=self;

self.tableView=table;

table.sectionFooterHeight=0;  //如果有代理的话,此行没有效果,以代理为先

table.sectionHeaderHeight=100;

[self.viewaddSubview:table];

}

#pragma mark - 懒加载

- (NSMutableArray *)procincesArray

{

if (_procincesArray ==nil)

{

NSString * path=[[NSBundlemainBundle]pathForResource:@"cities.plist"ofType:nil];

NSMutableDictionary * dic=[NSMutableDictionarydictionaryWithContentsOfFile:path];

_procincesArray = dic[@"provinces"];

}

return_procincesArray;

}

#pragma mark - 加载数据

- (void) _loadData

{

//dataArray的内容从 plist文件中取得

NSString * path=[[NSBundlemainBundle]pathForResource:@"cities.plist"ofType:nil];

NSMutableDictionary * dic=[NSMutableDictionarydictionaryWithContentsOfFile:path];

self.cityDic=dic[@"cities"];

self.procincesArray=dic[@"provinces"];

}

#pragma mark - UITableViewDatasource

//返回行数

- (NSInteger ) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

//NSLog(@"%@",self.procincesArray[section]);    //测试代码

NSString * tem =
self.procincesArray[section];

return ((NSArray *)(self.cityDic[tem])).count;

}

//返回cell

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath

{

static
NSString * identy=@"table";

UITableViewCell * cell=[tableViewdequeueReusableCellWithIdentifier:identy];

if (!cell)

{

cell=[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:identy];

}

NSString * path=[[NSBundlemainBundle]pathForResource:@"cities.plist"ofType:nil];

NSMutableDictionary * dic=[NSMutableDictionarydictionaryWithContentsOfFile:path];

self.proviNameStr=self.procincesArray[indexPath.section];

self.cityArray= (dic[@"cities"])[self.proviNameStr];

//    NSLog(@" %@--%@",self.proviNameStr, dic[self.proviNameStr]);

//    NSLog(@"%@",self.cityArray);

//    NSLog(@"%li", indexPath.section);

//    cell.textLabel.text=self.procincesArray[indexPath.row];

cell.textLabel.text=self.cityArray[indexPath.row];

return cell;

}

#pragma mark - 返回分组数量

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView

{

//NSLog(@"___++++%ld",self.procincesArray.count);

returnself.procincesArray.count;
 //分组数组长度

}

#pragma mark - UITableViewDelegate

//设置分组的头标题

- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

return
self.procincesArray[section];

}

#pragma mark - 设置头部的高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

return
60;

}

#pragma mark - 设置尾部的高度

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

{

return
0;

}

#pragma mark - 索引列

- (NSArray *) sectionIndexTitlesForTableView:(UITableView *)tableView

{

returnself.procincesArray;

}

- (void)didReceiveMemoryWarning {

[superdidReceiveMemoryWarning];

}

@end

时间: 2024-08-29 11:42:27

UITableView---分组 省份城市列表+ 右列索引的相关文章

中国省份城市列表(汉字+拼音)

-------------------中国省份和城市列表-------------------------- [ProvinceName] name.1=北京(BeiJing) name.2=上海(ShangHai) name.3=天津(TianJin) name.4=重庆(ChongQing) name.5=香港(XiangGang) name.6=澳门(Aomen) name.7=安徽(AnHui) name.8=福建(FuJian) name.9=广东(GuangDong) name.10

Android 快速索引(城市列表和联系人)

最近需要实现一个城市列表的快速索引功能.类似于联系人应用,根据姓名首字母快速索引功能. 要实现这个功能只需要解决两个问题: 1.对列表进行分组(具有同一特征),并且能够快速定位到该组的第一项 2.右侧分组'特征'快速索引栏的实现 第一个问题比较好解决,列表项可以根据相同的'特征'来分组,比如说城市列表可以根据相同首字母的城市名来进行分组. 如何来定位到分组的第一项,只需要把分组的'特征'和分组第一项下标关联起来,快速索引栏就能快速定位分组第一项 第二个问题可以通过自定义控件来实现,实现的形式有很

iOS高级控件之tableViewController-城市列表

今天我们要做的是一个城市信息的展示,先给大家看看是怎样的一个效果,我想应该是绝对的实用^_^ 好了,接下来我就简要的给大家讲讲步骤,因为技术博客写的真心不多,也不知道该怎样讲才能把每一个人都看懂,但是宝宝我本着写有态度的博客,尽量的将细节描述详尽,不对的地方,望指正. 哈哈 虽然感觉并没有多少人看,我想我应该是自己的忠实的粉丝^_^好了不说废话了,进入正题 首先,建立一个新的工程,然后将storyboard中的viewController删除,同时将左侧viewController.h和.m一并

Windows Phone 7 LongListSelector控件实现分类列表和字母索引

在wp7手机里面的联系人列表和程序里面里面我们可以看到一个根据字母索引来定位联系人或者应用程序的控件,那么这个控件就是LongListSelector控件了. LongListSelector是一种比ListBox更加强大的列表控件,你可以根据你列表的信息来分类排列,根据类别快速定位到你选中的类别的列表下,在数据量很大的情况下这种分类的优势很明显.LongListSelector可以自定义列表头,列表尾.类表头.列别尾等的样式和数据,可以实现各种个性化的列表样式和不同的数据的展现方式.Windo

【iOS开发之旅】UITableView示例-LOL英雄列表

UITableView示例-LOL英雄列表运行效果        CZHero.h // // CZHero.h // 04-UITableView示例-加载plist文件 // // Created by ChenQianPing on 16/1/21. // Copyright © 2016年 chenqp. All rights reserved. // #import <Foundation/Foundation.h> @interface CZHero : NSObject // 头

mysql 多列索引优化

Mysql所有的列都可以使用索引,.对相关列使用索引是提高SELECT操作性能的最佳途径.根据存储引擎定义每个表的最大索引数和最大索引长度.所有存储引擎支持每个表至少16个索引,总索引长度至少256字节.在索引中使用col_name(length)语法,可以创建一个只使用char和archar列的第一个length个字符的索引,按这种方式只索引列的前缀可以索引文件小的多.MyISAm和INNODb存储引擎还支持对blob和text列的索引,但是必须指定索引长度.fulltext索引用于全文搜索不

UIPickerView的简单应用——省份/城市选择的实现

UIPickerView的简单应用--省份/城市选择的实现 实现效果如图,左边为省份选择,右边选择省份对应的城市 数据plist形式如图 工程下载地址:工程下载 https://github.com/Nongchaozhe/UIPickerView-Province-city UIPickerView的实现重要还是两个代理协议中方法的实现 - (void)viewDidLoad { [super viewDidLoad]; [self loadData]; _pickView = [[UIPic

UIPickerView简单应用——省份/城市选择实现

UIPickerView的简单应用--省份/城市选择的实现 实现效果如图,左边为省份选择,右边选择省份对应的城市 数据plist形式如图 工程下载地址:工程下载 https://github.com/Nongchaozhe/UIPickerView-Province-city UIPickerView的实现重要还是两个代理协议中方法的实现 - (void)viewDidLoad { [super viewDidLoad]; [self loadData]; _pickView = [[UIPic

Android例子源码非第三方实现根据字母排序的城市列表

values 下dimens.xml <resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dime