iOS之城市列表
来源:互联网 发布:连接linux图形界面 编辑:程序博客网 时间:2024/05/15 23:46
有时候需要用到城市列表,作为地址选择,所以写了个demo。
核心代码:
#pragma mark - 获取城市数据-(void)getCityData{ NSString *path=[[NSBundle mainBundle] pathForResource:@"citydict" ofType:@"plist"]; self.cities = [NSMutableDictionary dictionaryWithContentsOfFile:path]; [self.keys addObjectsFromArray:[[self.cities allKeys] sortedArrayUsingSelector:@selector(compare:)]]; //添加热门城市 NSString *strHot = @"热"; [self.keys insertObject:strHot atIndex:0]; [self.cities setObject:_arrayHotCity forKey:strHot];}#pragma mark - tableView-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 20.0;}-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 20)]; bgView.backgroundColor = [UIColor lightGrayColor]; UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(13, 0, 250, 20)]; titleLabel.backgroundColor = [UIColor clearColor]; titleLabel.textColor = [UIColor blackColor]; titleLabel.font = [UIFont systemFontOfSize:12]; NSString *key = [_keys objectAtIndex:section]; if ([key rangeOfString:@"热"].location != NSNotFound) { titleLabel.text = @"热门城市"; } else titleLabel.text = key; [bgView addSubview:titleLabel]; return bgView;}- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{ return _keys;}- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ // Return the number of sections. return [_keys count];}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ // Return the number of rows in the section. NSString *key = [_keys objectAtIndex:section]; NSArray *citySection = [_cities objectForKey:key]; return [citySection count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *CellIdentifier = @"Cell"; NSString *key = [_keys objectAtIndex:indexPath.section]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ; cell.backgroundColor = [UIColor clearColor]; cell.contentView.backgroundColor = [UIColor clearColor]; cell.selectionStyle = UITableViewCellSelectionStyleNone; [cell.textLabel setTextColor:[UIColor blackColor]]; cell.textLabel.font = [UIFont systemFontOfSize:18]; } cell.textLabel.text = [[_cities objectForKey:key] objectAtIndex:indexPath.row]; return cell;}demo下载地址:点击打开链接
0 0
- iOS之城市列表
- 【代码笔记】iOS-plist获得城市列表
- iOS学习之分段Table View的使用(Grouped样式表格)城市列表
- Android之XmlPullParser解析城市列表XML
- 城市列表
- 城市列表
- 城市列表
- 城市列表
- 城市列表
- 城市列表
- 城市列表
- 城市列表
- 城市列表
- 城市列表
- 城市列表
- 城市列表
- 城市列表
- 城市列表
- 专有名词
- C plus plus 命名空间与 java package
- 详解制作跟文件系统
- Linux下jvm、tomcat、mysql、log4j优化配置笔记
- C语言结构体中加typedef的区别
- iOS之城市列表
- AF_INET6 socket / sock bigpicture
- Android自定义view字体居中
- QQ通信原理及QQ是怎么穿透内网进行通信的?
- 自定义Toast设置宽度
- BestCoder #37 Rikka with string (hdu 5205)
- Linux 安装与配置服务器版jre7
- 要打开数据库则必须使用 RESETLOGS 或 NORESETLOGS 选项
- AF_PACKET packet path