1. 程式人生 > >iOS開發UI篇—實現UITableview控件數據刷新

iOS開發UI篇—實現UITableview控件數據刷新

ani titles plist文件 mes 模型 source dequeue intro 自動

iOS開發UI篇—實現UITableview控件數據刷新

一、項目文件結構和plist文件

技術分享圖片

二、實現效果

1.說明:這是一個英雄展示界面,點擊選中行,可以修改改行英雄的名稱(完成數據刷新的操作).

運行界面:

技術分享圖片

點擊選中行:

技術分享圖片

修改數據後自動刷新:

技術分享圖片

三、代碼示例

數據模型部分:

YYheros.h文件

技術分享圖片

//
// YYheros.h
// 10-英雄展示(數據刷新)
//
// Created by apple on 14-5-29.
// Copyright (c) 2014年 itcase. All rights reserved.
//

#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;

//-(instancetype)initWithDict:(NSDictionary *)dict;
//+(instancetype)herosWithDict:(NSDictionary *)dict;
YYinitH(hero)
@end

技術分享圖片

YYheros.m文件

技術分享圖片

//
// YYheros.m
// 10-英雄展示(數據刷新)
//
// Created by apple on 14-5-29.
// Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYheros.h"

@implementation YYheros
//-(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];
//}
YYinitM(hero)
@end

技術分享圖片

主控制器 YYViewController.m文件

技術分享圖片

//
// YYViewController.m
// 10-英雄展示(數據刷新)
//
// Created by apple on 14-5-29.
// Copyright (c) 2014年 itcase. All rights reserved.
//

#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;
}
//每組每行的數據,設置cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//NSLog(@"cellForRowAtIndexPath 修改的了 %d", indexPath.row);
//1.去緩存中取
static NSString *identifier=@"hero";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
//2.如果沒有,那麽就自己創建
if (cell==nil) {
NSLog(@"chuangjiancell");
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
}
//3.設置數據

//3.1拿到該行的模型
YYheros *hero=self.heros[indexPath.row];
cell.textLabel.text=hero.name;
cell.imageView.image=[UIImage imageNamed:hero.icon];
cell.detailTextLabel.text=hero.intro;
//4.返回cell
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=UIAlertViewStyleSecureTextInput;
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
{
//1.修改模型
//如果選中的是取消,那麽就返回,不做任何操作
if (0==buttonIndex) return;
//否則就修改模型,刷新數據
YYheros *hero=self.heros[alertView.tag];

//拿到當前彈窗中的文本數據(已經修改後的數據)
UITextField *text=[alertView textFieldAtIndex:0];
//用修改後的數據去修改模型
hero.name=text.text;

//2.刷新數據
// 只要調用tableview的該方法就會自動重新調用數據源的所有方法
// 會自動調用numberOfSectionsInTableView
// 會自動調用numberOfRowsInSection
// 會自動調用cellForRowAtIndexPath
// [self.tableview reloadData];

// 刷新指定行
NSIndexPath *path = [NSIndexPath indexPathForRow:alertView.tag inSection:0];
[self.tableview reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationRight];
//如果不進行刷新會怎麽樣?(修改之後不會即時刷新,要等到重新對cell進行數據填充的時候才會刷新)
}
//隱藏狀態欄
-(BOOL)prefersStatusBarHidden
{
return YES;
}
@end

技術分享圖片

四、把常用的代碼封裝成一個帶參數的宏

封裝方法和代碼:

技術分享圖片

//
// Global.h
// 10-英雄展示(數據刷新)
//
// Created by apple on 14-5-29.
// Copyright (c) 2014年 itcase. All rights reserved.
//

#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

技術分享圖片

以後在需要使用的時候,只需要使用宏即可。

如在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];
}

技術分享圖片

五、註意點

1.刷新數據的兩個步驟:

1)修改模型

2)刷新表格數據(可以全部刷新,也可以刷新指定的行)

2.在主控制器文件中,遵守了三個協議

分別是:

UITableViewDataSource,

UIAlertViewDelegate,

UITableViewDelegate

iOS開發UI篇—實現UITableview控件數據刷新