首页 > 学院 > 开发设计 > 正文

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

2019-11-14 19:48:50
字体:
来源:转载
供稿:网友
A.需求
1.使用只有一个section的TableView来显示LOL 的英雄列表
2.内容包括标题、副标题、图标
3.使用plain样式
4.使用MVC模式
 
Image(74)
 
heros.plist 文件结构:
Image(75)
 
 
这个其实很简单,直接上代码了
 1 // 2 //  Hero.h 3 //  LOLHero 4 // 5 //  Created by hellovoidworld on 14/12/1. 6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8  9 #import <Foundation/Foundation.h>10 11 @interface Hero : NSObject12 13 @PRoperty(nonatomic, copy) NSString *icon;14 @property(nonatomic, copy) NSString *intro;15 @property(nonatomic, copy) NSString *name;16 17 - (instancetype) initWithDictionary:(NSDictionary *) dictionary;18 + (instancetype) heroWithDictionary:(NSDictionary *) dictionary;19 + (instancetype) hero;20 21 @end
 
 1 // 2 //  Hero.m 3 //  LOLHero 4 // 5 //  Created by hellovoidworld on 14/12/1. 6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8  9 #import "Hero.h"10 11 @implementation Hero12 13 - (instancetype) initWithDictionary:(NSDictionary *) dictionary {14     if (self = [super init]) {15         self.icon = dictionary[@"icon"];16         self.intro = dictionary[@"intro"];17         self.name = dictionary[@"name"];18     }19    20     return self;21 }22 23 + (instancetype) heroWithDictionary:(NSDictionary *) dictionary {24     return [[self alloc] initWithDictionary:dictionary];25 }26 27 + (instancetype) hero {28     return [self heroWithDictionary:nil];29 }30 31 @end
 
 1 // 2 //  ViewController.m 3 //  LOLHero 4 // 5 //  Created by hellovoidworld on 14/12/1. 6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8  9 #import "ViewController.h"10 #import "Hero.h"11 12 @interface ViewController () <UITableViewDataSource>13 14 // UITableView15 @property (weak, nonatomic) IBOutlet UITableView *tableView;16 17 // 所有hero资料18 @property(nonatomic, strong) NSArray *heros;19 20 @end21 22 @implementation ViewController23 24 - (void)viewDidLoad {25     [super viewDidLoad];26     // Do any additional setup after loading the view, typically from a nib.27    28     // 设置dataSource29     self.tableView.dataSource = self;30    31     // 设置行高32     self.tableView.rowHeight = 60;33 }34 35 - (void)didReceiveMemoryWarning {36     [super didReceiveMemoryWarning];37     // Dispose of any resources that can be recreated.38 }39 40 /** 隐藏状态栏 */41 - (BOOL)prefersstatusBarHidden {42     return YES;43 }44 45 /** 延迟加载hero数据 */46 - (NSArray *) heros {47     if (nil == _heros) {48         NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil]];49        50         NSMutableArray *herosArray = [NSMutableArray array];51         for (NSDictionary *dict in dictArray) {52             Hero *hero = [Hero heroWithDictionary:dict];53             [herosArray addObject:hero];54         }55        56         _heros = herosArray;57     }58    59     return _heros;60 }61 62 #pragma mark - 列表方法63 64 // section数, 默认是165 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {66     return 1;67 }68 69 // 特定section的行数70 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {71     return self.heros.count;72 }73 74 75 // 特定行的内容76 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {77     Hero *hero = self.heros[indexPath.row];78    79     // 必须使用"UITableViewCellStyleSubtitle"才能显示副标题80     UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil];81 82     // 标题83     cell.textLabel.text = hero.name;84    85     // 副标题86     cell.detailTextLabel.text = hero.intro;87    88     // 图标89     cell.imageView.image = [UIImage imageNamed:hero.icon];90    91     return cell;92 }93 94 @end

 


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表