[iOS基礎控制項,ios基礎控制項

來源:互聯網
上載者:User

[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

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.