[iOS基礎控制項,ios基礎控制項
A.需求1.以LOL英雄列表為藍本,給其加上即時修改英雄名稱的功能2.使用UIAlertView3.全域重新整理reloadData4.局部重新整理 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 // 代替上方代碼, 使用KVC19 [self setValuesForKeysWithDictionary:dictionary];20 }21 22 return self;23 }24 25 + (instancetype) heroWithDictionary:(NSDictionary *) dictionary {26 return [[self alloc] initWithDictionary:dictionary];27 }28 29 + (instancetype) hero {30 return [self heroWithDictionary:nil];31 }32 33 @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 // 遵守了UITableViewDelegate才能響應行點擊事件, 遵守UIAlertViewDelegate處理彈窗的事件 13 @interface ViewController () <UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate> 14 15 // UITableView 16 @property (weak, nonatomic) IBOutlet UITableView *tableView; 17 18 // 所有hero資料 19 @property(nonatomic, strong) NSArray *heros; 20 21 @end 22 23 @implementation ViewController 24 25 - (void)viewDidLoad { 26 [super viewDidLoad]; 27 // Do any additional setup after loading the view, typically from a nib. 28 29 // 設定dataSource 30 self.tableView.dataSource = self; 31 32 // 設定行高 33 self.tableView.rowHeight = 60; 34 35 // 設定delegate 36 self.tableView.delegate = self; 37 } 38 39 - (void)didReceiveMemoryWarning { 40 [super didReceiveMemoryWarning]; 41 // Dispose of any resources that can be recreated. 42 } 43 44 /** 隱藏狀態列 */ 45 - (BOOL)prefersStatusBarHidden { 46 return YES; 47 } 48 49 /** 消極式載入hero資料 */ 50 - (NSArray *) heros { 51 if (nil == _heros) { 52 NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil]]; 53 54 NSMutableArray *herosArray = [NSMutableArray array]; 55 for (NSDictionary *dict in dictArray) { 56 Hero *hero = [Hero heroWithDictionary:dict]; 57 [herosArray addObject:hero]; 58 } 59 60 _heros = herosArray; 61 } 62 63 return _heros; 64 } 65 66 #pragma mark - 列表方法 67 68 // section數, 預設是1 69 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 70 return 1; 71 } 72 73 // 特定section的行數 74 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 75 return self.heros.count; 76 } 77 78 79 // 特定行的內容 80 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 81 Hero *hero = self.heros[indexPath.row]; 82 83 // 使用static修飾局部變數,能保證只分配一次儲存空間 84 static NSString *hereCellId = @"hero"; 85 86 // 使用特定標識符, 從緩衝池查看時候有適合的cell存在 87 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:hereCellId]; 88 89 if (nil == cell) { 90 // 建立的時候指定標識符 91 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:hereCellId]; 92 } 93 94 // 標題 95 cell.textLabel.text = hero.name; 96 97 // 副標題 98 cell.detailTextLabel.text = hero.intro; 99 100 // 表徵圖101 cell.imageView.image = [UIImage imageNamed:hero.icon];102 103 return cell;104 }105 106 #pragma mark - 響應事件107 108 /** 點擊選中某行 */109 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {110 // 取得model111 Hero *hero = self.heros[indexPath.row];112 113 // 彈窗114 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"修改英雄名稱" message:nil delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];115 116 // 設定delegate,也可以在init方法中指定117 alertView.delegate = self;118 119 // 設定彈窗樣式120 alertView.alertViewStyle = UIAlertViewStylePlainTextInput;121 122 // 設定彈窗輸入框內容123 [alertView textFieldAtIndex:0].text = hero.name;124 125 // 儲存index126 alertView.tag = indexPath.row;127 128 // 顯示彈窗129 [alertView show];130 }131 132 /** 處理彈窗點擊“確定”按鈕事件 */133 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {134 // 取消135 if (0 == buttonIndex) return;136 137 // 確定138 // 儲存新的名字到model139 Hero *hero = self.heros[alertView.tag];140 hero.name = [alertView textFieldAtIndex:0].text;141 142 // 重新整理資料143 // 1.全域重新整理144 // [self.tableView reloadData];145 146 // 2.局部重新整理147 NSIndexPath *indexPath = [NSIndexPath indexPathForRow:alertView.tag inSection:0];148 [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];149 150 }151 152 @end