[iOS基礎控制項,ios基礎控制項
A.需求1.使用只有一個section的TableView來顯示LOL 的英雄列表2.內容包括標題、副標題、表徵圖3.使用plain樣式4.使用MVC模式 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