表视图的简单使用-UITableView
2015-08-20 20:28
344 查看
//
// ViewController.m
// city
//
// Created by imac on 15/8/20.
// Copyright (c) 2015年
刘
. All rights reserved.
//
#import "ViewController.h"
@interface
ViewController ()<UITableViewDataSource>
@property(nonatomic,strong)NSDictionary *dataDic;
@property(nonatomic,strong)NSArray *dataArray;
@property(nonatomic,strong)UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_tableView = [[UITableView
alloc] initWithFrame:CGRectMake(0,
20,
self.view.frame.size.width,
self.view.frame.size.height)
style:UITableViewStylePlain];
// _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
[self.view
addSubview:_tableView];
_tableView.dataSource =
self;
[self
dataArray];
[self
dataDic];
}
#pragma mark 城市数据
-(NSDictionary *)dataDic{
NSString *path = [[NSBundle
mainBundle] pathForResource:@"cities.plist"
ofType:nil];
_dataDic = [NSDictionary
dictionaryWithContentsOfFile:path];
return
_dataDic;
}
#pragma mark 省级数据
-(NSArray *)dataArray{
NSString *path = [[NSBundle
mainBundle]pathForResource:@"provinces.plist"
ofType:nil];
_dataArray = [NSArray
arrayWithContentsOfFile:path];
return
_dataArray;
}
#pragma mark 分组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return
_dataArray.count;
}
#pragma mark 每组个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSString *key = _dataArray[section];
NSArray *arrTemp = [NSArray
array];
arrTemp = [_dataDic
valueForKey:key];
return arrTemp.count;
}
#pragma mark 每行显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{
UITableViewCell *cell = [[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
NSString *key = _dataArray[indexPath.section];
NSArray *arrTemp = [NSArray
array];
arrTemp = [_dataDic
valueForKey:key];
cell.textLabel.text =
arrTemp[indexPath.row];
return cell;
}
#pragma mark 每组的标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return _dataArray[section];
}
@end
需要使用到两个plist的文件,进行本地数据加载
// ViewController.m
// city
//
// Created by imac on 15/8/20.
// Copyright (c) 2015年
刘
. All rights reserved.
//
#import "ViewController.h"
@interface
ViewController ()<UITableViewDataSource>
@property(nonatomic,strong)NSDictionary *dataDic;
@property(nonatomic,strong)NSArray *dataArray;
@property(nonatomic,strong)UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_tableView = [[UITableView
alloc] initWithFrame:CGRectMake(0,
20,
self.view.frame.size.width,
self.view.frame.size.height)
style:UITableViewStylePlain];
// _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
[self.view
addSubview:_tableView];
_tableView.dataSource =
self;
[self
dataArray];
[self
dataDic];
}
#pragma mark 城市数据
-(NSDictionary *)dataDic{
NSString *path = [[NSBundle
mainBundle] pathForResource:@"cities.plist"
ofType:nil];
_dataDic = [NSDictionary
dictionaryWithContentsOfFile:path];
return
_dataDic;
}
#pragma mark 省级数据
-(NSArray *)dataArray{
NSString *path = [[NSBundle
mainBundle]pathForResource:@"provinces.plist"
ofType:nil];
_dataArray = [NSArray
arrayWithContentsOfFile:path];
return
_dataArray;
}
#pragma mark 分组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return
_dataArray.count;
}
#pragma mark 每组个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSString *key = _dataArray[section];
NSArray *arrTemp = [NSArray
array];
arrTemp = [_dataDic
valueForKey:key];
return arrTemp.count;
}
#pragma mark 每行显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath{
UITableViewCell *cell = [[UITableViewCell
alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
NSString *key = _dataArray[indexPath.section];
NSArray *arrTemp = [NSArray
array];
arrTemp = [_dataDic
valueForKey:key];
cell.textLabel.text =
arrTemp[indexPath.row];
return cell;
}
#pragma mark 每组的标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return _dataArray[section];
}
@end
需要使用到两个plist的文件,进行本地数据加载
相关文章推荐
- GUI - Web前端开发框架
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 评价ui设计作品好坏的八个标准(界面/交互设计研究)
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 为Yahoo! UI Extensions Grid增加内置的可编辑器
- Cocos2d-x UI开发之文本类使用实例
- 在IE 浏览器中使用 jquery的fadeIn() 效果 英文字符字体加粗
- UI Events 用户界面事件
- 基于jQuery UI CSS Framework开发Widget的经验
- jquery ui 1.7 ui.tabs 动态添加与关闭(按钮关闭+双击关闭)
- jQuery UI的Dialog无法提交问题的解决方法
- 关于jQuery UI 使用心得及技巧
- jQuery ui 1.7更新小结
- 从零开始学习jQuery (十) jQueryUI常用功能实战
- jquery ui对话框实例代码