UITableView

#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、value1、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);
    //跳转,传值都在这个方法内部完成。
}

posted on 2016-02-21 08:36  哥依然帅气  阅读(115)  评论(0编辑  收藏  举报

导航