iOS開發UI篇—實現UItableview控件數據刷新緩存
1、項目文件結構和plist文件app
2、實現效果ide
1.說明:這是一個英雄展現界面,點擊選中行,能夠修改改行英雄的名稱(完成數據刷新的操做).atom
運行界面:spa
點擊選中行:3d
修改數據後自動刷新:code
3、代碼示例blog
數據模型部分:開發
YYheros.h文件it
1 // 2 // YYheros.h 3 // 10-英雄展現(數據刷新) 4 // 5 // Created by apple on 14-5-29. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 #import "Global.h" 11 12 @interface YYheros : NSObject 13 @property(nonatomic,copy)NSString *name; 14 @property(nonatomic,copy)NSString *icon; 15 @property(nonatomic,copy)NSString *intro; 16 17 //-(instancetype)initWithDict:(NSDictionary *)dict; 18 //+(instancetype)herosWithDict:(NSDictionary *)dict; 19 YYinitH(hero) 20 @end
YYheros.m文件
1 // 2 // YYheros.m 3 // 10-英雄展現(數據刷新) 4 // 5 // Created by apple on 14-5-29. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYheros.h" 10 11 @implementation YYheros 12 //-(instancetype)initWithDict:(NSDictionary *)dict 13 //{ 14 // if (self=[super init]) { 15 //// self.name=dict[@"name"]; 16 //// self.icon=dict[@"icon"]; 17 //// self.intro=dict[@"intro"]; 18 // 19 // //使用KVC 20 // [self setValuesForKeysWithDictionary:dict]; 21 // } 22 // return self; 23 //} 24 // 25 //+(instancetype)herosWithDict:(NSDictionary *)dict 26 //{ 27 // return [[self alloc]initWithDict:dict]; 28 //} 29 YYinitM(hero) 30 @end
主控制器 YYViewController.m文件
1 // 2 // YYViewController.m 3 // 10-英雄展現(數據刷新) 4 // 5 // Created by apple on 14-5-29. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 #import "YYheros.h" 11 12 @interface YYViewController ()<UITableViewDataSource,UIAlertViewDelegate,UITableViewDelegate> 13 @property (strong, nonatomic) IBOutlet UITableView *tableview; 14 @property(nonatomic,strong)NSArray *heros; 15 @end 16 17 @implementation YYViewController 18 19 - (void)viewDidLoad 20 { 21 [super viewDidLoad]; 22 //設置數據源 23 self.tableview.dataSource=self; 24 self.tableview.delegate=self; 25 self.tableview.rowHeight=60.f; 26 NSLog(@"%d",self.heros.count); 27 } 28 29 #pragma mark -懶加載 30 -(NSArray *)heros 31 { 32 if (_heros==nil) { 33 34 NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"heros.plist" ofType:nil]; 35 NSArray *temparray=[NSArray arrayWithContentsOfFile:fullpath]; 36 37 NSMutableArray *arrayM=[NSMutableArray array]; 38 for (NSDictionary *dict in temparray) { 39 YYheros *hero=[YYheros herosWithDict:dict]; 40 [arrayM addObject:hero]; 41 } 42 _heros=[arrayM mutableCopy]; 43 } 44 return _heros; 45 } 46 47 #pragma mark- tableview的處理 48 //多少組 49 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 50 { 51 return 1; 52 } 53 //多少行 54 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 55 { 56 return self.heros.count; 57 } 58 //每組每行的數據,設置cell 59 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 60 { 61 //NSLog(@"cellForRowAtIndexPath 修改的了 %d", indexPath.row); 62 //1.去緩存中取 63 static NSString *identifier=@"hero"; 64 UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier]; 65 //2.若是沒有,那麼就本身建立 66 if (cell==nil) { 67 NSLog(@"chuangjiancell"); 68 cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier]; 69 } 70 //3.設置數據 71 72 //3.1拿到該行的模型 73 YYheros *hero=self.heros[indexPath.row]; 74 cell.textLabel.text=hero.name; 75 cell.imageView.image=[UIImage imageNamed:hero.icon]; 76 cell.detailTextLabel.text=hero.intro; 77 //4.返回cell 78 return cell; 79 } 80 81 #pragma mark-數據刷新 82 //1.彈出窗口,拿到數據 83 //當某一行被選中的時候調用該方法 84 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 85 { 86 //拿到改行的數據模型 87 YYheros *hero=self.heros[indexPath.row]; 88 UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"修改數據" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"肯定", nil]; 89 90 //密碼框形式的 91 //alert.alertViewStyle=UIAlertViewStyleSecureTextInput; 92 alert.alertViewStyle=UIAlertViewStylePlainTextInput; 93 UITextField *text=[alert textFieldAtIndex:0]; 94 //把當前行的英雄數據顯示到文本框中 95 text.text=hero.name; 96 alert.tag=indexPath.row; 97 [alert show]; 98 } 99 //2.修改數據,完成刷新操做 100 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 101 { 102 //1.修改模型 103 //若是選中的是取消,那麼就返回,不作任何操做 104 if (0==buttonIndex) return; 105 //不然就修改模型,刷新數據 106 YYheros *hero=self.heros[alertView.tag]; 107 108 //拿到當前彈窗中的文本數據(已經修改後的數據) 109 UITextField *text=[alertView textFieldAtIndex:0]; 110 //用修改後的數據去修改模型 111 hero.name=text.text; 112 113 //2.刷新數據 114 // 只要調用tableview的該方法就會自動從新調用數據源的全部方法 115 // 會自動調用numberOfSectionsInTableView 116 // 會自動調用numberOfRowsInSection 117 // 會自動調用cellForRowAtIndexPath 118 // [self.tableview reloadData]; 119 120 // 刷新指定行 121 NSIndexPath *path = [NSIndexPath indexPathForRow:alertView.tag inSection:0]; 122 [self.tableview reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationRight]; 123 //若是不進行刷新會怎麼樣?(修改以後不會即時刷新,要等到從新對cell進行數據填充的時候纔會刷新) 124 } 125 //隱藏狀態欄 126 -(BOOL)prefersStatusBarHidden 127 { 128 return YES; 129 } 130 @end
4、把經常使用的代碼封裝成一個帶參數的宏
封裝方法和代碼:
1 // 2 // Global.h 3 // 10-英雄展現(數據刷新) 4 // 5 // Created by apple on 14-5-29. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #ifndef _0____________Global_h 10 #define _0____________Global_h 11 12 /** 13 * 自定義帶參數的宏 14 */ 15 #define YYinitH(name) -(instancetype)initWithDict:(NSDictionary *)dict;\ 16 +(instancetype)herosWithDict:(NSDictionary *)dict; 17 18 19 #define YYinitM(name) -(instancetype)initWithDict:(NSDictionary *)dict\ 20 {\ 21 if (self=[super init]) {\ 22 [self setValuesForKeysWithDictionary:dict];\ 23 }\ 24 return self;\ 25 }\ 26 \ 27 +(instancetype)herosWithDict:(NSDictionary *)dict\ 28 {\ 29 return [[self alloc]initWithDict:dict];\ 30 }\ 31 32 #endif
之後在須要使用的時候,只須要使用宏便可。
如在YYheros.m文件中使用YYinitM(hero)這一句代碼能夠代替下面的代碼段:
-(instancetype)initWithDict:(NSDictionary *)dict { if (self=[super init]) { // self.name=dict[@"name"]; // self.icon=dict[@"icon"]; // self.intro=dict[@"intro"]; //使用KVC [self setValuesForKeysWithDictionary:dict]; } return self; } +(instancetype)herosWithDict:(NSDictionary *)dict { return [[self alloc]initWithDict:dict]; }
5、注意點
1.刷新數據的兩個步驟:
1)修改模型
2)刷新表格數據(能夠所有刷新,也能夠刷新指定的行)
2.在主控制器文件中,遵照了三個協議
分別是:
UITableViewDataSource,
UIAlertViewDelegate,
UITableViewDelegate