ios-表视图-demo5-索引
时间:2014-04-28 15:11:25
收藏:0
阅读:656
#import "RootViewController.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]]; // 排序 _keyArray = [[keyArray sortedArrayUsingSelector:@selector(compare:)] retain]; _tableView = [[UITableView alloc] initWithFrame:view.bounds style:UITableViewStylePlain]; _tableView.dataSource = self; // 设置数据源 _tableView.delegate = self; // 设置委托 [self.view addSubview:_tableView]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - TableView Datasource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [_keyArray count]; } // 表视图当中存在secion的个数,默认是1个 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; { NSArray *data = [_dataDic objectForKey:[_keyArray objectAtIndex:section]]; return [data count]; } // section 中包含row的数量 - (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:[_keyArray 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 _keyArray[section]; } - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return _keyArray; } // 返回索引的内容 - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { NSLog(@"index : %d title : %@", index, title); return index;//根据数组是索引内容,根据下表来取得跳转区域,默认也是跳转到下表坐标 } // 选中时,跳转表视图 #pragma mark - UITableViewDelegate - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 60; } - (void)dealloc { [_tableView release]; _tableView = nil; [super dealloc]; } @end
评论(0)