1 //
2 // ViewController.m
3 // 03-LOL英雄
4 //
5 // Created by apple on 15-8-1.
6 // Copyright (c) 2015年 apple. All rights reserved.
7 //
8
9 #import "ViewController.h"
10 #import "CZHero.h"
11 /*
12 懒加载数据
13 1.导入模型的头文件
14 2.定义一个数组属性,用来存放数据
15 3.懒加载数据
16 */
17
18 //1.遵守UITableViewDelegate
19 @interface ViewController () <UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>
20 // 2.定义一个数组属性,用来存放数据
21 @property (nonatomic,strong) NSArray *heros;
22
23 @property (weak, nonatomic) IBOutlet UITableView *tableView;
24
25 //记录需要修改indexPath
26 @property (nonatomic,strong) NSIndexPath *indexPath;
27
28 @end
29
30 @implementation ViewController
31
32 - (void)viewDidLoad {
33 [super viewDidLoad];
34 // Do any additional setup after loading the view, typically from a nib.
35 // 测试
36 // NSLog(@"%@",self.heros);
37
38 // 设置tableView的数据源为控制器
39 self.tableView.dataSource = self;
40
41 // 2.设置tableView的代理是控制器
42 self.tableView.delegate = self;
43
44
45 // 设置tableView行高,每一个cell的行高都是一样的!!
46 self.tableView.rowHeight = 100;
47
48
49 }
50
51 #pragma mark - 实现数据源方法
52 // 默认返回1,可以不实现
53 //- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
54 //{
55 // return 1;
56 //}
57
58
59 //一共有多少行
60 - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
61 {
62 return self.heros.count;
63 }
64
65 //每一行显示什么内容
66
67 -(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
68 {
69 NSLog(@"%s,section = %zd,row = %zd",__func__,indexPath.section,indexPath.row);
70 // 1创建cell
71 // UITableViewCell *cell = [[UITableViewCell alloc] init];
72 //
73 // indexPath.section;
74 // indexPath.row;
75
76
77 // 在创建cell之前首先去缓冲池(房间)取可重用的cell
78 // dequeue 出列(从缓冲池中取出一个cell),注意这个重用标示必须和创建cell的时候的重用标示一样
79 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
80
81 // 如果缓冲池中没有可以重用cell,才去创建一个新的cell
82 if (cell == nil) {
83 cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"UITableViewCell"];
84 // NSLog(@"%s,section = %zd,row = %zd",__func__,indexPath.section,indexPath.row);
85 }
86
87 // 注意给cell设置的代码一定要放到if语句的外部
88 // 2给cell设置数据
89 // 2.1 取出模型数据
90 CZHero *hero = self.heros[indexPath.row];
91 // 2.2 把模型数据设置给cell
92 // 设置cell的头像
93 cell.imageView.image = [UIImage imageNamed:hero.icon];
94 // 设置cell标题
95 cell.textLabel.text = hero.name;
96 // cell.textLabel.textColor = [UIColor whiteColor];
97 // 设置cell描述信息 detail详情
98 cell.detailTextLabel.text = hero.intro;
99 // 让lable自动换行
100 cell.detailTextLabel.numberOfLines = 0;
101
102
103 // 清除cell的背景颜色
104 cell.backgroundColor = [UIColor clearColor];
105
106 // 返回cell
107 return cell;
108 }
109
110 #pragma mark - 代理方法
111
112 //当用户选中某一行的时候,会调用的代理方法
113 - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
114 {
115
116 // 记录用户选中indexPath
117 self.indexPath = indexPath;
118
119 // NSLog(@"%s section = %zd,row = %zd",__func__,indexPath.section,indexPath.row);
120
121 CZHero *hero = self.heros[indexPath.row];
122
123 // 创建UIAlertView
124 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"修改" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
125
126 // 设置alertView类型
127 alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
128 // 取出textField
129 UITextField *textField = [alertView textFieldAtIndex:0];
130
131 // 设置数据
132 textField.text = hero.name;
133
134
135 // 展示alertView
136 [alertView show];
137 }
138
139 #pragma mark 实现alertView的代理方法
140
141 //当点击alertView上的按钮的时候,执行这个方法
142 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
143 {
144 // 如果点击了确定按钮,修改tableView上
145 if (buttonIndex == 1) {
146 /*
147 错误的方式,当这个cell划出屏幕在划出来得时候,数据就又恢复过来了
148 // 取出indexPath对应cell(要修改cell)
149 UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:self.indexPath];
150 // 取出alertView上的textField
151 UITextField *textField = [alertView textFieldAtIndex:0];
152 // 把textField上的文本设置cell
153 cell.textLabel.text = textField.text;
154 */
155 // 正确的方式:
156
157 // 1.修改indexPath对应模型对象
158 // 1.取出当前indexPath对应模型数据
159 CZHero *hero = self.heros[self.indexPath.row];
160
161 // 2.取出alertView上的textField
162 UITextField *textField = [alertView textFieldAtIndex:0];
163 // 3. 修改模型数据
164 hero.name = textField.text;
165
166 // 2.刷新表格,
167 // 1 刷新表格第一种方式.让表格重新加载(在开发中用这个就可以了),该方法会刷新整个界面
168 // [self.tableView reloadData];
169
170 // 2.刷新表格的第二种方式:只刷新一行的
171 // 它只能创建一个不可变数组
172 // NSArray *array = @[self.indexPath];
173 // NSArray *array1 = [NSArray arrayWithObjects:self.indexPath, nil];
174
175 // 该方法只会刷新指定行
176 [self.tableView reloadRowsAtIndexPaths:@[self.indexPath] withRowAnimation:UITableViewRowAnimationRight];
177
178 }
179 }
180
181 #pragma mark - 懒加载数据
182 - (NSArray *)heros
183 {
184 if (_heros == nil) {
185 _heros = [CZHero heros];
186 }
187 return _heros;
188 }
189
190 @end