[iOS基础控件-6.2]LOL英雄列表UITableView单项显示

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

A.需求

1.使用只有一个section的TableView来显示LOL 的英雄列表

2.内容包括标题、副标题、图标

3.使用plain样式

4.使用MVC模式

heros.plist 文件结构:

这个其实很简单,直接上代码了

1//2//Hero.h3//LOLHero4//5//Created by hellovoidworld on 14/12/1.6//Copyright (c) 2014年 hellovoidworld. All rights reserved.7//89#import<Foundation/Foundation.h>1011@interfaceHero : NSObject1213@PRoperty(nonatomic, copy) NSString *icon;14@property(nonatomic, copy) NSString *intro;15@property(nonatomic, copy) NSString *name;1617- (instancetype) initWithDictionary:(NSDictionary *) dictionary;18+ (instancetype) heroWithDictionary:(NSDictionary *) dictionary;19+(instancetype) hero;2021@end

1//2//Hero.m3//LOLHero4//5//Created by hellovoidworld on 14/12/1.6//Copyright (c) 2014年 hellovoidworld. All rights reserved.7//89#import"Hero.h"1011@implementationHero1213- (instancetype) initWithDictionary:(NSDictionary *) dictionary {14if(self =[super init]) {15self.icon = dictionary[@"icon"];16self.intro = dictionary[@"intro"];17self.name = dictionary[@"name"];18}1920returnself;21}2223+ (instancetype) heroWithDictionary:(NSDictionary *) dictionary {24return[[self alloc] initWithDictionary:dictionary];25}2627+(instancetype) hero {28return[self heroWithDictionary:nil];29}3031@end

1//2//ViewController.m3//LOLHero4//5//Created by hellovoidworld on 14/12/1.6//Copyright (c) 2014年 hellovoidworld. All rights reserved.7//89#import"ViewController.h"10#import"Hero.h"1112@interfaceViewController () <UITableViewDataSource>1314//UITableView15@property (weak, nonatomic) IBOutlet UITableView *tableView;1617//所有hero资料18@property(nonatomic, strong) NSArray *heros;1920@end2122@implementationViewController2324- (void)viewDidLoad {25[super viewDidLoad];26//Do any additional setup after loading the view, typically from a nib.2728//设置dataSource29self.tableView.dataSource =self;3031//设置行高32self.tableView.rowHeight =60;33}3435- (void)didReceiveMemoryWarning {36[super didReceiveMemoryWarning];37//Dispose of any resources that can be recreated.38}3940/** 隐藏状态栏*/41-(BOOL)prefersstatusBarHidden {42returnYES;43}4445/** 延迟加载hero数据*/46- (NSArray *) heros {47if(nil ==_heros) {48NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"heros.plist"ofType:nil]];4950NSMutableArray *herosArray =[NSMutableArray array];51for(NSDictionary *dictindictArray) {52Hero *hero =[Hero heroWithDictionary:dict];53[herosArray addObject:hero];54}5556_heros =herosArray;57}5859return_heros;60}6162#pragmamark - 列表方法6364//section数, 默认是165- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {66return1;67}6869//特定section的行数70- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {71returnself.heros.count;72}737475//特定行的内容76- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {77Hero *hero =self.heros[indexPath.row];7879//必须使用"UITableViewCellStyleSubtitle"才能显示副标题80UITableViewCell *cell =[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];8182//标题83cell.textLabel.text =hero.name;8485//副标题86cell.detailTextLabel.text =hero.intro;8788//图标89cell.imageView.image =[UIImage imageNamed:hero.icon];9091returncell;92}9394@end

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航