// .h 文件
#import <UIKit/UIKit.h>
#define kDeviceHeight [UIScreen mainScreen].bounds.size.height;
@interface RootViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
@private
UITableView *_tableView;
NSArray *_listArray;
NSDictionary *_dataDic;
}
@end
// .m 文件
#import "RootViewController.h"
#import "DetailViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)loadView {
// 创建一个基本视图
UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
// 设置为当前控制器的默认视图
self.view = view;
// 释放视图
[view release];
NSString *path = [[NSBundle mainBundle] pathForResource:@"ListData" ofType:@"plist"];
_dataDic = [[NSDictionary dictionaryWithContentsOfFile:path] retain];
NSArray *keyArray = [[NSArray arrayWithArray:[_dataDic allKeys]] retain];
_listArray = [[keyArray sortedArrayUsingSelector:@selector(compare:)] retain];
NSLog(@"%@", _listArray);
// 创建tableview
_tableView = [[UITableView alloc] initWithFrame:view.bounds style:UITableViewStylePlain];
// 设置数据源 注意这个方法设置不对,没有数据哦!
_tableView.dataSource = self;
// 设置表格的代理
_tableView.delegate = self;
// 设置表视图cell的高度
_tableView.rowHeight = 70;
// 添加到默认控制器中
[self.view addSubview:_tableView];
}
// 表视图sescations有多少个
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
return [_listArray count];
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *arr = [_dataDic objectForKey:[_listArray objectAtIndex:section]];
return [arr count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 定义一个静态标识符
static NSString *cellIdentifier = @"cell";
// 检查是否限制单元格
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
// 创建单元格
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}
// 给cell内容赋值
NSArray *data = [_dataDic objectForKey:[_listArray objectAtIndex:indexPath.section]];
NSString *fontName = [data objectAtIndex:indexPath.row];
cell.textLabel.text = fontName;
cell.textLabel.font = [UIFont systemFontOfSize:18];
return cell;
}
// 索引主标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return _listArray[section];
}
// 副标题
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return _listArray;
} // 返回索引的内容
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
NSLog(@"index : %d title : %@", index, title);
return index;
} // 选中时,跳转表视图
- (void) dealloc {
[_listArray release];
[super dealloc];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
// ListData.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>A</key>
<array>
<string>Australia</string>
<string>Argentina</string>
<string>American</string>
<string>Afghanistan</string>
</array>
<key>B</key>
<array>
<string>Brazil</string>
<string>Belgium</string>
<string>Bermuda</string>
</array>
<key>C</key>
<array>
<string>China</string>
<string>Canada</string>
<string>Congo</string>
<string>Cameroon</string>
<string>Chad</string>
</array>
<key>D</key>
<array>
<string>Denmark</string>
<string>Djibouti</string>
</array>
<key>E</key>
<array>
<string>England</string>
<string>Egypt</string>
<string>Estonia</string>
</array>
<key>F</key>
<array>
<string>France</string>
<string>Finland</string>
</array>
<key>G</key>
<array>
<string>Germany</string>
<string>Greece</string>
<string>Georgia</string>
<string>Guinea</string>
</array>
</dict>
</plist>