#import "ViewController.h"
#import "Student.h"
@interface ViewController ()
@property (nonatomic, retain) NSMutableArray *dataArray;
@property (nonatomic, retain) NSMutableArray *dogArray;
@property (nonatomic, retain) NSMutableArray *huluArray;
@property (nonatomic, retain) NSMutableArray *beautyArray;
//存儲模型數據的數組,做爲表視圖的數據提供者
@property (nonatomic, retain) NSMutableArray *modelArray;
@end
@implementation ViewController
- (void)dealloc {
[_modelArray release];
[_dogArray release];
[_huluArray release];
[_beautyArray release];
[_dataArray release];
[super dealloc];
}
//讀取數據
- (void)handleData {
//獲取到plist文件在包中的路徑
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student.plist" ofType:@"nil"];
//從路徑中獲得一個數組
NSArray *array = [NSArray arrayWithContentsOfFile:filePath];
NSLog(@"array is %@",array);
//初始化模型數組
_modelArray = [[NSMutableArray alloc] initWithCapacity:0];
for (NSDictionary *dic in array) {
//將每個字典轉換爲模型,將模型加到模型數組中去
Student *student = [[Student alloc] init];
[student setValuesForKeysWithDictionary:dic];
[_modelArray addObject:student];
[student release];
}
NSLog(@"_modelArray is %@",_modelArray);
}
- (void)viewDidLoad {
[super viewDidLoad];
[self handleData];
_dogArray = [[NSMutableArray alloc] initWithObjects:@"金毛", @"拉布拉多", @"中華田園犬", @"鬥牛犬", @"沙皮", @"哈士奇", @"薩摩耶", nil];
_huluArray = [[NSMutableArray alloc] initWithObjects:@"太虛", @"巴達", @"富富", @"洪荒", @"中二", @"嘴嘴", @"霖霖", nil];
_beautyArray = [[NSMutableArray alloc] initWithObjects:@"姜麗玲", @"王越亞", @"鄭殊", @"胡潔佩", @"代苗苗", @"朱珍潔", @"莫婉依", nil];
_dataArray = [[NSMutableArray alloc] initWithObjects:_dogArray, _huluArray, _beautyArray, nil];
//數據數組初始化
// _dataArray = [[NSMutableArray alloc] initWithObjects:@"中二", @"巴達", @"洪荒", @"富富", @"嘴嘴", @"秋香", @"太虛", @"胡潔佩", @"霖霖", nil];
//建立一個表視圖
UITableView *tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
// [tableView reloadData];
//分割線樣式
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
//分割線顏色
tableView.separatorColor = [UIColor redColor];
//指定cell的高度
// tableView.rowHeight = 150;
//tableView若是想顯示數據,必須接收UITableViewDataSource協議,而且實現協議中的兩個方法
//指定代理
tableView.dataSource = self;
tableView.delegate = self;
//將表視圖添加到根視圖上進行顯示
[self.view addSubview:tableView];
//釋放內存
[tableView release];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark -----數據源協議中必須實現的方法-----
//指定表視圖分區個數
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [_dataArray count];
}// Default is 1 if not implemented
/*
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
switch (section) {
case 0:
return [NSString stringWithFormat:@"gougou,分區:%ld",section];
break;
case 1:
return [NSString stringWithFormat:@"葫蘆娃,分區:%ld",section];
break;
default:
return [NSString stringWithFormat:@"七仙女,分區:%ld",section];
break;
}
}*/
//設置每一個分區中顯示多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [_dataArray[section] count];//返回數組容量
}
//設置每行顯示什麼內容,也就是指定每一行的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//建立一個重用標識符
static NSString *reuseIdentifier = @"reuse";
//表視圖經過重用標識去重用池中查找是否有可以被重用的cell 緩存池
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (cell == nil) {
//建立一個cell
/**
cell樣式有四種
default、subtitle、value一、value2
*/
NSLog(@"建立了一個新的cell");
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
//指定cell內部控件的顯示內容
//重用池中取出的cell,並無釋放,因此會保留原有的內容,若是想要顯示本身的信息,須要對cell內部的控件進行從新賦值
cell.imageView.image = [UIImage imageNamed:@"IMG_0419.JPG"];
cell.textLabel.text = _dataArray[indexPath.section][indexPath.row];
// cell.textLabel.text = @"中二洪荒巴達小峯峯";
// cell.detailTextLabel.text = @"富富嘴嘴霖霖";
return cell;
}
#pragma mark -----代理協議-----
//返回分區頭的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 60;
}
//指定分區頭顯示的視圖
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
switch (section) {
case 0:{
UILabel *label = [[UILabel alloc] init];
label.text = [NSString stringWithFormat:@"狗狗,分區:%ld",section];
label.textAlignment = NSTextAlignmentCenter;
return [label autorelease];
break;}
case 1:{
UILabel *label = [[UILabel alloc] init];
label.text = [NSString stringWithFormat:@"葫蘆娃,分區:%ld",section];
label.textAlignment = NSTextAlignmentCenter;
return [label autorelease];
break;
}
default:{
UILabel *label = [[UILabel alloc] init];
label.text = [NSString stringWithFormat:@"七仙女,分區:%ld",section];
label.textAlignment = NSTextAlignmentCenter;
return [label autorelease];
break;}
}
}
//指定每一行的高度,也就是每一個cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 150;
}
//指定右側邊欄索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return @[@"單", @"娃", @"女"];
}
//點擊單元格以後觸發的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"%ld, %ld",indexPath.section, indexPath.row);
//跳轉,傳值都在這個方法內部完成。
}
數組