、、、、
plist文件如下:
将相应图片资源拉进工程中一个自设的文件夹,在Supporting files下新建model类如下:
参照文件中字典的属性,编辑Hero.h如下:
// // Hero.h // LOL英雄榜UITableView加载plist展示单组数据 // // Created by apple on 15/8/31. // Copyright (c) 2015年 LiuXun. All rights reserved. // #import <Foundation/Foundation.h> @interface Hero : NSObject @property (nonatomic, strong) NSString *icon; @property (nonatomic, strong) NSString *intro; @property (nonatomic, strong) NSString *name; -(instancetype)initWithDict:(NSDictionary *)dict; +(instancetype)heroWithDict:(NSDictionary *)dict; @end编辑Hero.m如下:
// // Hero.m // LOL英雄榜UITableView加载plist展示单组数据 // // Created by apple on 15/8/31. // Copyright (c) 2015年 LiuXun. All rights reserved. // #import "Hero.h" @implementation Hero -(instancetype)initWithDict:(NSDictionary *)dict { if (self = [super init]) { // self.name = dict[@"name"]; // self.icon = dict[@"icon"]; // self.intro = dict[@"intro"]; [self setValuesForKeysWithDictionary:dict]; // KVC } return self; } +(instancetype)heroWithDict:(NSDictionary *)dict { return [[self alloc] initWithDict:dict]; } @end编辑控制器的.h文件如下:
// // ViewController.h // LOL英雄榜UITableView加载plist展示单组数据 // // Created by apple on 15/8/31. // Copyright (c) 2015年 LiuXun. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController <UITableViewDataSource,UITableViewDelegate> @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSArray *array; @end编辑控制器的.m文件如下:
// // ViewController.m // LOL英雄榜UITableView加载plist展示单组数据 // // Created by apple on 15/8/31. // Copyright (c) 2015年 LiuXun. All rights reserved. // #import "ViewController.h" #import "Hero.h" #define WIDTH [UIScreen mainScreen].bounds.size.width #define HEIGHT [UIScreen mainScreen].bounds.size.height @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 添加UITableView控件 self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain]; self.tableView.dataSource = self; self.tableView.showsVerticalScrollIndicator = NO; // 取消竖直滑动条 self.tableView.rowHeight = 70; // 只对于所有行高度都相同的情况下 [self.view addSubview:self.tableView]; } // 重写数组的get方法实现懒加载 -(NSArray *)array { if (_array == nil) { //查找plist文件的路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil]; // 根据路径读取plist文件到一个数组 NSArray *arrayDict = [NSArray arrayWithContentsOfFile:path]; // 新建空的一个专门存放model对象的可变数组 NSMutableArray *arrayModel = [NSMutableArray array]; // 遍历从文件读取存放所有字典的数组,逐个转成model对象存放到可变数组中 for (NSDictionary * dict in arrayDict){ // 新建一个model对象 Hero *hero = [Hero heroWithDict:dict]; [arrayModel addObject:hero]; } _array = arrayModel; } return _array; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } // 取消状态栏 -(BOOL)prefersStatusBarHidden { return YES; } // 利用UITableViewDelegate代理协议中的方法,即使行高不相同的也行 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.row %2 == 0) { return 60; }else{ return 80; } } -(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { return 1; } -(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.array.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 新建指定格式的单元格对象 UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]; // 获取model数据 Hero *hero = self.array[indexPath.row]; cell.imageView.image = [UIImage imageNamed:hero.icon]; cell.textLabel.text = hero.name; cell.detailTextLabel.text = hero.intro; // 添加箭头 /* accessoryType 的枚举值如下: UITableViewCellAccessoryNone, // don't show any accessory view UITableViewCellAccessoryDisclosureIndicator, // regular chevron. doesn't track UITableViewCellAccessoryDetailDisclosureButton, // info button w/ chevron. tracks UITableViewCellAccessoryCheckmark, // checkmark. doesn't track UITableViewCellAccessoryDetailButton NS_ENUM_AVAILABLE_IOS(7_0) // info button. tracks*/ if(indexPath.row <1){ cell.accessoryType = UITableViewCellAccessoryNone; // 默认状态下不显示任何组件 }else if(indexPath.row<2){ cell.accessoryType = UITableViewCellAccessoryDetailButton ; //显示详细信息按钮 }else if (indexPath.row<3){ cell.accessoryType = UITableViewCellAccessoryCheckmark; // 添加选择按钮 }else if (indexPath.row <4){ cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; // 添加箭头按钮 }else if (indexPath .row<5){ cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; // 同时添加箭头与详细信息的按钮 }else if (indexPath.row <6 ){ cell.accessoryView = [[UISwitch alloc] init]; }else cell.accessoryView = [[UIButton alloc] init]; return cell; } @end运行结果如下: