标签:des style blog io strong 文件 for ar 数据
对TableView进一步理解
1. 创建项目。-- 省市级联小例子。
2. 添加.plist文件。可以理解为数据字典。键值对(Key - Value)
2.1 cities.plist - 城市
2.2 provinces.plist - 省份
3. 常用方法:
3.1 QQ,微信,通讯录常见的,列表右侧有:ABCDE... 字样,就是如下方法。
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
3.2 点击某一行执行方法(要遵守UITableViewDelegate协议)
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
#import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITableViewDataSource, UITableViewDelegate> @end
#import "ViewController.h"
@interface ViewController ()
//城市字典
@property (strong, nonatomic) NSDictionary * arrCities;
//省数组
@property (strong, nonatomic) NSArray * arrProvinces;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//初始化
NSBundle * bundle = [NSBundle mainBundle];
self.arrCities = [NSDictionary dictionaryWithContentsOfFile:[bundle pathForResource:@"cities" ofType:@"plist"]];
self.arrProvinces = [NSArray arrayWithContentsOfFile:[bundle pathForResource:@"provinces" ofType:@"plist"]];
}
#pragma mark - tableView DataSource
#pragma mark 分组数量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.arrProvinces.count;
}
#pragma mark 每组行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSString * provinceName = self.arrProvinces[section];
NSArray * arrCityName =[self.arrCities objectForKey:provinceName];
return arrCityName.count;
}
#pragma mark 每行内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
NSString * provinceName = self.arrProvinces[indexPath.section];
NSArray * arrCityNames = [self.arrCities objectForKey:provinceName];
cell.textLabel.text = arrCityNames[indexPath.row];
return cell;
}
#pragma mark 头部
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return self.arrProvinces[section];
}
#pragma mark 分组信息
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.arrProvinces;
}
#pragma mark - tableView Delegate
#pragma mark 点击一行
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString * provinceName = self.arrProvinces[indexPath.section];
NSArray * arrCityNames = [self.arrCities objectForKey:provinceName];
NSLog(@"%@", arrCityNames[indexPath.row]);
}
@end
介绍一个调试的方法:
用处:
1. 数组, log时,出现UTF-8编码。
2. 对象,log时,出现指针地址等等。
总结:
1. 创建一个分组。命名:Log,继承NSArray
2. 就可以看到,新建出 名为:NSArrar+Log 文件。
3. 重写父类的描述方法。代码如下:
#import "NSArray+log.h"
@implementation NSArray (log)
- (NSString *)descriptionWithLocale:(id)locale
{
NSMutableString * strDescript = [NSMutableString string];
[strDescript appendString:@"("];
[strDescript appendFormat:@"(数量总数==%i)", self.count];
for (id obj in self)
{
[strDescript appendFormat:@"\n\t\"%@\",", obj]; //格式化数组格式。
}
[strDescript appendString:@")"];
return strDescript;
}
@end
4. 外面的正常log数组就可以。
NSLog(@"%@", self.arrProvinces);
5. 截图如下:
消化下。。。。 继续前进。。。。
标签:des style blog io strong 文件 for ar 数据
原文地址:http://www.cnblogs.com/iCodePhone/p/3937172.html