實現 UITableView 的數據刷新
一、項目結構與 plist:

二、效果說明:這是一個英雄展示界面,點擊選中某一行後,可以修改該行英雄的名稱,從而完成數據刷新。
運行界面:

點擊選中行:

修改數據後自動刷新:

三、代碼示例
數據模型 YYheros.h:
#import <Foundation/Foundation.h> #import "Global.h" @interface YYheros : NSObject @property(nonatomic,copy)NSString *name; @property(nonatomic,copy)NSString *icon; @property(nonatomic,copy)NSString *intro; YYinitH(hero) @end
YYheros.m(借助自定義宏快速生成字典轉模型的方法):
#import "YYheros.h" @implementation YYheros YYinitM(hero) @end
主控制器 YYViewController.m:
#import "YYViewController.h"
#import "YYheros.h"
@interface YYViewController ()<UITableViewDataSource,UIAlertViewDelegate,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *tableview;
@property(nonatomic,strong)NSArray *heros;
@end
@implementation YYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableview.dataSource=self;
self.tableview.delegate=self;
self.tableview.rowHeight=60.f;
NSLog(@"%d",self.heros.count);
}
#pragma mark - 懶加載
-(NSArray *)heros
{
if (_heros==nil) {
NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"heros.plist" ofType:nil];
NSArray *temparray=[NSArray arrayWithContentsOfFile:fullpath];
NSMutableArray *arrayM=[NSMutableArray array];
for (NSDictionary *dict in temparray) {
YYheros *hero=[YYheros herosWithDict:dict];
[arrayM addObject:hero];
}
_heros=[arrayM mutableCopy];
}
return _heros;
}
#pragma mark- tableview 的處理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.heros.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 1. 去緩存中取
static NSString *identifier=@"hero";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
// 2. 若沒有則自己創建
if (cell==nil) {
NSLog(@"chuangjiancell");
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
// 3. 設置數據
YYheros *hero=self.heros[indexPath.row];
cell.textLabel.text=hero.name;
cell.imageView.image=[UIImage imageNamed:hero.icon];
cell.detailTextLabel.text=hero.intro;
return cell;
}
#pragma mark- 數據刷新
// 1. 彈出窗口,拿到數據
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
YYheros *hero=self.heros[indexPath.row];
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"修改數據" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
// 普通文本輸入框
alert.alertViewStyle=UIAlertViewStylePlainTextInput;
UITextField *text=[alert textFieldAtIndex:0];
// 把當前行數據顯示到輸入框
text.text=hero.name;
alert.tag=indexPath.row;
[alert show];
}
// 2. 修改數據,完成刷新
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// 點取消則直接返回
if (0==buttonIndex) return;
YYheros *hero=self.heros[alertView.tag];
UITextField *text=[alertView textFieldAtIndex:0];
hero.name=text.text;
// 刷新指定行(也可改用 [self.tableview reloadData] 全部刷新)
NSIndexPath *path = [NSIndexPath indexPathForRow:alertView.tag inSection:0];
[self.tableview reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationRight];
// 若不刷新,修改後不會即時生效,要等下次填充數據時才會體現
}
-(BOOL)prefersStatusBarHidden
{
return YES;
}
@end
四、把常用代碼封裝成帶參數的宏
字典轉模型的寫法幾乎每個模型都要重複一遍,可以把它封裝成自定義宏,放到 Global.h 中:
#ifndef _0____________Global_h
#define _0____________Global_h
/** 自定義帶參數的宏 */
#define YYinitH(name) -(instancetype)initWithDict:(NSDictionary *)dict;\
+(instancetype)herosWithDict:(NSDictionary *)dict;
#define YYinitM(name) -(instancetype)initWithDict:(NSDictionary *)dict\
{\
if (self=[super init]) {\
[self setValuesForKeysWithDictionary:dict];\
}\
return self;\
}\
+(instancetype)herosWithDict:(NSDictionary *)dict\
{\
return [[self alloc]initWithDict:dict];\
}\
#endif
以後只要在 .m 文件裡寫一句 YYinitM(hero),就能替代整段 initWithDict: 與 herosWithDict: 的實現。
五、注意點
1. 刷新數據分兩步:先修改模型,再刷新表格(可全部刷新,也可只刷新指定行);
2. 主控制器遵守了三個協議:UITableViewDataSource、UIAlertViewDelegate、UITableViewDelegate。
一、使用步驟其實只有三步:
1. 告訴它一共有多少組:numberOfSectionsInTableView:;
2. 告訴它每組有多少行:tableView:numberOfRowsInSection:;
3. 設置每組每行(cell):tableView:cellForRowAtIndexPath:。
二、使用說明:
1. 組數與行數通常與數據息息相關,數據多存放在 plist 中,需以模型形式加載進項目;
2. 設置 cell 三步走:創建 cell(考慮復用與緩存)、為 cell 設數據、返回 cell。
設置 cell 有三種方式:
(1)直接用系統自帶的 UITableViewCell;
(2)通過 xib 自定義,適用於長相一致的场景,如團購列表;
(3)通過純代碼自定義,適用於差異較大的场景,如行高不一、部分 cell 才有的屬性,如微博列表。
三、自定義 cell 的兩種做法:
1. xib 方式:新建 xib 描述樣式;新建繼承自系統 view 的自定義類(類名最好與 xib 一致);把 xib 控件與 .m 連線;提供一個類方法返回創建好的實例;提供模型屬性並重寫其 setter,把數據展示到子控件上。
2. 純代碼方式:新建繼承自 UITableViewCell 的類;重寫 initWithStyle:reuseIdentifier: 添加並初始化子控件;提供數據模型與 frame 模型;cell 只持有 frame 模型;重寫 frame 模型的 setter 設置數據與 frame;frame 模型採用懶加載,每個 cell 只加載一次。
四、使用代理的步驟:
(1)先弄清誰是誰的代理;
(2)定義代理協議,命名規範為「控件類名 + Delegate」;
(3)定義代理方法,一般標為 @optional,方法名以控件名開頭,至少有一個參數把控件本身傳出去;
(4)設置代理對象,並讓其遵守協議、實現方法;
(5)在恰當時刻調用代理方法通知事件(調用前先判斷對方是否實現了該方法)。