源码下载地址:http://download.csdn.net/detail/liu537192/8463517
效果图:
核心代码:
//
// LiuJieViewController.m
// 04-数据刷新
//
// Created by XinYou on 15-2-28.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "LiuJieViewController.h"
#import "LiuJieHero.h"
@interface LiuJieViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)NSArray *heros;
@end
@implementation LiuJieViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 给UITableView设置数据源
self.tableView.dataSource = self;
// 给UITableView设置代理
self.tableView.delegate = self;
// 设置行高
self.tableView.rowHeight = 60;
}
/**
* 隐藏状态栏
*/
- (BOOL)prefersStatusBarHidden{
return YES;
}
- (NSArray *)heros{
if (_heros == nil) {
// 获取plist文件的路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil];
// 加载数组
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
// 将dictArray中得所有字典对象转成模型对象,放到新的数组中
NSMutableArray *tempArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
// 创建模型对象
LiuJieHero *hero = [LiuJieHero heroWithDict:dict];
// 添加模型对象到数组中
[tempArray addObject:hero];
}
// 赋值
_heros = tempArray;
}
return _heros;
}
#pragma mark -数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.heros.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// static修饰局部变量:可以保证局部变量只分配一次存储空间(只初始化一次)
static NSString *ID = @"hero";
// 1.通过一个标识去缓存池中寻找可循环利用的cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 2.如果没有可循环利用的cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
// 3.给cell设置新的数据
LiuJieHero *hero = self.heros[indexPath.row];
cell.imageView.image = [UIImage imageNamed:hero.icon];
cell.textLabel.text = hero.name;
cell.detailTextLabel.text = hero.intro;
return cell;
}
#pragma mark -代理方法
/**
* 当用户点击了某一行,就会执行这个方法
*/
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// NSLog(@"点击了第%d行",indexPath.row);
// 取得被点击这行对应的模型
LiuJieHero *hero = self.heros[indexPath.row];
// 创建弹框
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"数据展示" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
// 设置对话框的类型
// UIAlertViewStyleDefault 默认的样式的对话框
// UIAlertViewStyleSecureTextInput 带有一个密文输入框的对话框
// UIAlertViewStylePlainTextInput 带有一个普通输入框的对话框
// UIAlertViewStyleLoginAndPasswordInput 带有两个输入框(一个普通和一个密文)的对话框
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
// 设置对话框中输入框的文本
[alert textFieldAtIndex:0].text = hero.name;
// 显示弹框
[alert show];
// 绑定行号到alertView上
alert.tag = indexPath.row;
}
#pragma mark -alertView的代理方法
/**
* 点击了alertView上面的按钮就会调用这个方法
*/
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
// 如果按钮的索引为0,表示点击了“取消”按钮
if (buttonIndex == 0) {
return;
}
// 1 取得文本输入框最后的文字
NSString *name = [alertView textFieldAtIndex:0].text;
// 2 修改模型数据
int row = alertView.tag;
LiuJieHero *hero = self.heros[row];
hero.name = name;
// 3 告诉tableView重新加载模型数据
// reloadData : tableView会向数据源重新请求数据
// 重新调用数据源的相应方法取得数据
// 重新调用数据源的tableView:numberOfRowsInSection:获得行数
// 重新调用数据源的tableView:cellForRowAtIndexPath:得知每一行显示怎样的cell
// 全部刷新
// [self.tableView reloadData];
NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];
// 3 局部刷新
[self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationBottom];
}
@end
//
// LiuJieHero.h
// 04-数据刷新
//
// Created by XinYou on 15-2-28.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import
@interface LiuJieHero : NSObject
/**
* 每个英雄对应的图片
*/
@property (nonatomic,copy) NSString *icon;
/**
* 每个英雄对应的名字
*/
@property (nonatomic,copy) NSString *name;
/**
* 每个英雄的介绍
*/
@property (nonatomic,copy) NSString *intro;
+ (instancetype)heroWithDict:(NSDictionary *)dict;
- (instancetype)initWithDict:(NSDictionary *)dict;
@end
//
// LiuJieHero.m
// 04-数据刷新
//
// Created by XinYou on 15-2-28.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "LiuJieHero.h"
@implementation LiuJieHero
- (instancetype)initWithDict:(NSDictionary *)dict{
if (self = [super init]) {
self.icon = dict[@"icon"];
self.name = dict[@"name"];
self.intro = dict[@"intro"];
// 也可以使用KVC
// [self setValuesForKeysWithDictionary:dict];
}
return self;
}
+ (instancetype)heroWithDict:(NSDictionary *)dict{
return [[self alloc] initWithDict:dict];
}
@end