车的品牌展示:
// Car.h // 07-UITableView练习-Car // // Created by 瞿杰 on 15/9/28. // #import <Foundation/Foundation.h> @interface Car : NSObject @property (nonatomic,copy)NSString * icon; @property (nonatomic,copy)NSString * name; + (instancetype)carWithDictionary:(NSDictionary *)dic; - (instancetype)initWithDictionary:(NSDictionary *)dic; @end
// // Car.m // 07-UITableView练习-Car // // Created by 瞿杰 on 15/9/28. // #import "Car.h" @implementation Car + (instancetype)carWithDictionary:(NSDictionary *)dic{ return [[self alloc]initWithDictionary:dic]; } - (instancetype)initWithDictionary:(NSDictionary *)dic{ if (self = [super init]) { self.icon = dic[@"icon"]; self.name = dic[@"name"]; } return self; } @end
// // CarGroup.h // 07-UITableView练习-Car // // Created by 瞿杰 on 15/9/28. // #import <Foundation/Foundation.h> @interface CarGroup : NSObject @property (nonatomic,copy)NSString * title; @property(nonatomic, strong)NSArray * cars; + (instancetype)carGroupWithDictionary:(NSDictionary * ) dic; - (instancetype)initWithDictionary:(NSDictionary * )dic; @end
// // CarGroup.m // 07-UITableView练习-Car // // Created by 瞿杰 on 15/9/28. // Copyright © 2015年 itcast. All rights reserved. // #import "CarGroup.h" #import "Car.h" @implementation CarGroup + (instancetype)carGroupWithDictionary:(NSDictionary * ) dic{ return [[self alloc]initWithDictionary:dic]; } - (instancetype)initWithDictionary:(NSDictionary * )dic{ if (self = [super init]) { self.title = dic[@"title"]; NSArray * carsDic = dic[@"cars"]; NSMutableArray * cars = [NSMutableArray array]; for(NSDictionary * dict in carsDic){ Car * car = [Car carWithDictionary:dict]; [cars addObject:car]; } self.cars = cars ; } return self; } @end
// // ViewController.m // 07-UITableView练习-Car // // Created by 瞿杰 on 15/9/28. // Copyright © 2015年 itcast. All rights reserved. // #import "ViewController.h" #import "CarGroup.h" #import "Car.h" @interface ViewController ()<UITableViewDataSource , UITableViewDelegate ,UIAlertViewDelegate> @property (nonatomic , strong)NSArray * carGroup; @property (weak, nonatomic) IBOutlet UITableView *tableView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } // 隐藏状态栏 - (BOOL)prefersStatusBarHidden{ return YES; } // 获得cars_total.plist中的数据 - (NSArray *)carGroup{ if (nil == _carGroup) { NSString * path = [[NSBundle mainBundle] pathForResource:@"cars_total.plist" ofType:nil]; NSArray * group = [NSArray arrayWithContentsOfFile:path]; NSMutableArray * carGroup = [NSMutableArray array]; for(NSDictionary * dic in group){ CarGroup * tmp = [CarGroup carGroupWithDictionary:dic]; [carGroup addObject:tmp]; } self.carGroup = carGroup ; } return _carGroup; } #pragma mark - 数据源协议UIDataSource中的方法 /** 1.返回组数 */ - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return self.carGroup.count ; } /** 2.返回第section组的行数 */ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ CarGroup * group = self.carGroup[section]; return group.cars.count; } /** 3.返回显示第indexPath.section行第 indexPath.row行的内容 */ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ //(1)可重用的标识符 NSString * ID = @"carGroup"; // (2)先从数据池中看有没有同一类型的Cell UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID]; //(3)如果没有可重用的cell,就创建一个 if (cell == nil) { NSLog(@"-----(%ld , %ld)",indexPath.section,indexPath.row); cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID]; } //(4)设置cell中的内容 CarGroup * group = self.carGroup[indexPath.section]; Car * car = group.cars[indexPath.row]; cell.textLabel.text = car.name; cell.imageView.image = [UIImage imageNamed:car.icon]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell; } /** 返回第section组的头标题*/ - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ CarGroup * group = self.carGroup[section]; return group.title; } //- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{ // //} /** 返回每组的头标题,用于显示在屏幕的最右边 */ - (NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{ // NSMutableArray * titleArray = [NSMutableArray array]; // for(CarGroup * group in self.carGroup){ // [titleArray addObject:group.title]; // } // return titleArray; return [self.carGroup valueForKey:@"title"]; } #pragma mark - UITableViewDelegate代理协议中的方法 /** 选择点击了indexPath的cell,可做一些事*/ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ CarGroup * group = self.carGroup[indexPath.section]; Car * car = group.cars[indexPath.row]; // 弹出一个UIAlertView窗口 UIAlertView * alert = [[UIAlertView alloc]initWithTitle:car.name message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; [alert show]; //可实现alert的UIAlertViewDelegate代理协议中的方法,当对alert操作时,可做相应的反应 } - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{ } /** * 每一行的高度不一致的时候使用这个方法来设置行高 */ - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row == 0) return 100; return 60; } //#pragma mark - alertView的代理方法 ///** // * 点击了alertView上面的按钮就会调用这个方法 // * // * @param buttonIndex 按钮的索引,从0开始 // */ //- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex //{ // if (buttonIndex == 0) return; // // // 按钮的索引肯定不是0 // // // 1.取得文本框最后的文字 // NSString *name = [alertView textFieldAtIndex:0].text; // // int row = alertView.tag; // // NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0]; // // UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:path]; // // cell.textLabel.text = name; // // // 2.修改模型数据 // long int row = alertView.tag; // CarGroup *carGroup = self.carGroup[row]; // carGroup.title = name; // // // 3.告诉tableView重新加载模型数据 // // reloadData : tableView会向数据源重新请求数据 // // 重新调用数据源的相应方法取得数据 // // 重新调用数据源的tableView:numberOfRowsInSection:获得行数 // // 重新调用数据源的tableView:cellForRowAtIndexPath:得知每一行显示怎样的cell // // 全部刷新 // // [self.tableView reloadData]; // // // 局部刷新 // NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0]; // [self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationBottom]; //} @end
LOL英雄头像展示:
#import <Foundation/Foundation.h> @interface QJHero : NSObject @property (nonatomic , copy)NSString * icon; @property (nonatomic , copy)NSString * name; @property (nonatomic , copy)NSString * intro; + (instancetype)heroWithDictionary:(NSDictionary *)dic; - (instancetype)initWithDictionary:(NSDictionary *)dic; @end
#import "QJHero.h" @implementation QJHero + (instancetype)heroWithDictionary:(NSDictionary *)dic{ return [[self alloc]initWithDictionary:dic]; } - (instancetype)initWithDictionary:(NSDictionary *)dic{ if (self = [super init]) { self.icon = dic[@"icon"]; self.name = dic[@"name"]; self.intro = dic[@"intro"]; } return self ; } @end
// // ViewController.m // 06-UITableView练习-LOL // // Created by 瞿杰 on 15/9/27. // #import "ViewController.h" #import "QJHero.h" @interface ViewController ()<UITableViewDataSource> @property (weak, nonatomic) IBOutlet UITableView *tableView; @property (nonatomic , strong) NSArray * heros; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // 设置每行的行高 self.tableView.rowHeight = 50; } - (BOOL)prefersStatusBarHidden{ return YES ; } - (NSArray *)heros{ if (_heros == nil) { NSString * path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil]; NSArray * heros = [NSArray arrayWithContentsOfFile:path]; NSMutableArray * heroArray = [[NSMutableArray alloc]init]; for(NSDictionary * dic in heros){ QJHero * hero = [QJHero heroWithDictionary:dic]; [heroArray addObject:hero]; } _heros = heroArray ; } return _heros ; } // 1.返回组数 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 1; } // 2.返回第section的行数 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return self.heros.count; } // 3.返回显示第indexPath.section行第 indexPath.row行的内容 - (UITableViewCell * )tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ NSString * ID = @"A"; // 先从数据池中看有没有同一类型的Cell UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 如果没有就创建一个 if (cell == nil) { NSLog(@"创建一个 --- %ld",(long)indexPath.row); cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; } // 设置cell的数据内容 QJHero * hero = self.heros[indexPath.row]; cell.textLabel.text = hero.name; //cell.textLabel.backgroundColor = [UIColor redColor]; cell.detailTextLabel.text = hero.intro; cell.imageView.image = [UIImage imageNamed:hero.icon]; // 设置每行后面的配件类型为 > cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // 设置背景颜色 cell.backgroundColor = [UIColor redColor]; return cell ; } @end