tableView編輯

  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
相關文章
相關標籤/搜索