<a target=_blank href="http://download.csdn.net/detail/vaercly/7908395" target="_blank" rel="external nofollow" >点击打开下载链接</a>
//
// RootViewController.m
// LessonUITabelView
//
// Created by lanouhn on 14-9-3.
// Copyright (c) 2014年 [email protected] 陈聪雷. All rights reserved.
//
#import "RootViewController.h"
#import "StudentCell.h"
@interface RootViewController ()<UITableViewDataSource, UITableViewDelegate>
//存储有一一对应关系的数据时吗要用字典存储
@property (nonatomic, retain) NSDictionary *names;
@property (nonatomic, retain) NSArray *titles;//存储分区的title
@end
@implementation RootViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
NSArray *firstGroup = @[@"聪雷", @"小明",@"小狗"];
NSArray *secondGroup = @[@"聪从",@"小猫",@"笑天",@"飞飞"];
NSArray *thirdGroup = @[@"天天",@"亮亮", @"aa", @"bb", @"晓光",@"小明"];
self.names = @{@"A": firstGroup, @"D": secondGroup, @"C": thirdGroup};
//取出字典中的key, 进行升序排序
self.titles = [[self.names allKeys] sortedArrayUsingSelector:@selector(compare:)];
}
//初始化时加载数据
[self readDataFromPlist];
return self;
}
- (void)readDataFromPlist
{
//1 获取文件路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"plist"];
//2 根据文件路径初始化一个OC中的字典对象
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
NSLog(@"%@", dic);
}
- (void)loadView
{
//tabelView 表视图, 是iOS中用来显示以及能够编辑一系列具有相同数据结构的信息列表的控件, UITabelView继承自UIScrollView, 所以能够滑动, 但是只能在垂直方向滑动, 而且只有一列, UITabelView是有分区(section, 对应班级学生的分组), 以及列(row, 对应一个组的成员), 对于分区和行的索引值都是从0开始的, 若想获取一行(获取某个班的某个人), 要根据分区所在分区的行(根据学生所在的分组和组中的位置(第几个人))决定, 而且两个信息都存储在NSIndexPath类的对象中, UITabelView的每一行都是一个UITabelViewCell类型的对象,每一个cell上都包含imageView(left), label(textLabel, detailLabel, center), accessoryView(right)
UITableView *tabelView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
//设置分割线的样式
tabelView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
tabelView.separatorColor = [UIColor grayColor];
//指定tabelView数据源, 为tabelView显示提供数据支持, 需要服从UITabelViewDataSource协议
tabelView.dataSource = self;
//设置tableView的代理(来处理cell的点击事件)
tabelView.delegate = self;
//设置tableView行高
tabelView.rowHeight = 70;
self.view = tabelView;
[tabelView release];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.navigationItem.title = @"蓝鸥9班";
}
//设置tabelView的行数(每个分组的行数)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// //1 先获取key值
// NSString *key = self.titles[section];
// //2 根据key值, 获取对应的数组
// NSArray *group = self.names[key];
// //3 求数组的元素个数
// return group.count;
// return [self.names[self.titles[section]] count];
return 1000;
}
//用来创建cell, 每一行都要对应一个cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
// //1 先获取key
// NSString *key = self.titles[indexPath.section];
// //2 根据key获取value(数组)
// NSArray *group = self.names[key];
// //3 从数组中去元素
// cell.textLabel.text = group[indexPath.row];
// cell.textLabel.text = self.names[self.titles[indexPath.section]][indexPath.row];
// cell.textLabel.text = [[self.names objectForKey:[self.titles objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
//1 创建重用标示符
static NSString *identifier = @"lanou7ban";
//2 根据标示符去重用队列取出可重用的cell
StudentCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
//3 判断是否成功取到可重用的cell
if (!cell) {
//若没有成功取到可重用的cell, 就创建一个cell
//创建完cell之后一定要给cell贴一个重用标示符, 方便别人重用
cell = [[[StudentCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier] autorelease];
}
cell.textLabel.text = @"vaercly";
cell.detailTextLabel.text = @"frank";
return cell;
}
#pragma mark - UITableViewDelegate
//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//偶数的cell高度为100
//奇数的cell高度为50;
if (indexPath.row % 2) {
return 100;
}
return 50;
}
//当cell被选中是触发
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"%d", indexPath.row);
}
#pragma mark - UITableViewDataSourse
/*
//返回tableView分区的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.names.count;
}
//设置每个分区头显示的文字
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return self.titles[section];
}
//设置tableView左边的索引值 (用来快速定位分区, 方便查找), 要和每个分区的title对应上
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return self.titles;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
*/
- (void)dealloc
{
self.names = nil;
[super dealloc];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end