//
// ViewController.m
// 英雄展示
//
// Created by 刘宏帅 on 15/3/22.
// Copyright (c) 2015年 dugu. All rights reserved.
//
#import "ViewController.h"
#import "LHShero.h"
@interface ViewController () <UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
@property (nonatomic,strong) NSArray *heros;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//设置行高
self.tableView.rowHeight = 60;
}
/**
* 加载数据
*/
- (NSArray *)heros
{
if(_heros == nil){
//初始化
//1.获得plist全路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil];
//2.加载数组
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
//3.将dictArray里面的字典转成模型对象,放在新的数组里
NSMutableArray *heroArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
//3.1创建模型对象
LHShero *hero =[LHShero heroWithDict:dict];
//3.2添加模型对象到新的数组中
[heroArray addObject:hero];
}
//4.赋值
_heros = heroArray;
}
return _heros;
}
- (BOOL)prefersStatusBarHidden
{
return YES;
}
#pragma mark - 数据源方法
/**
* 设置行数
*/
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.heros.count;
}
/**
* 调用cell
*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//
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设置新的数据
//取出模型
LHShero *hero = self.heros[indexPath.row];
//设置新cell数据
cell.textLabel.text = hero.name;
cell.detailTextLabel.text = hero.intro;
cell.imageView.image = [UIImage imageNamed:hero.icon];
return cell;
}
#pragma mark -代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//1.取得被点击着一行的模型
LHShero *hero = self.heros[indexPath.row];
//2.弹框
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"数据展示" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//3.设置对话框
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
//4.取得唯一的文本框,显示英雄的名称
[alert textFieldAtIndex:0].text = hero.name;
[alert show];
//5.绑定行号到alerView上
alert.tag = indexPath.row;
}
#pragma mark - alertView的代理方法
/**
* 点击上方的按钮就调用这个方法
buttonIndex 按钮的索引,从0开始
*/
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) return;
//1.取得文本框的最后文字
NSString *name = [alertView textFieldAtIndex:0].text;
//2.修改数据模型
int row = alertView.tag;
LHShero *hero = self.heros[row];
hero.name = name;
//3 告诉tableView重新加载数据模型
//reloadData : tableView会向数据源请求刷新数据
//重新调用数据源的方法取得数据
//重新调用数据源的tableView:numberOfRowsInSection:获得行数
//重新调用数据源方法tableView:cellForRowAtIndexPath:得知每一行显示怎样的cell
//全部刷新
//局部刷新
NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0];
[self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationAutomatic];
}
@end
代理方法,针对于数据刷新和弹框