·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> [iOS基础控件-6.5]UITableView的数据刷新

[iOS基础控件-6.5]UITableView的数据刷新

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23
A.需求 1.以LOL英雄列表为蓝本,给其加上实时修改英雄名称的功能 2.使用UIAlertView 3.全局刷新reloadData 4.局部刷新 Image(79)Image(80)     B.实现 1.使用UIAlertView     // 弹窗     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"修改英雄名称" message:nil delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];   2.全局刷新reloadData     // 刷新数据
    // 1.全局刷新    [self.tableView reloadData];   3.局部刷新     // 2.局部刷新
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:alertView.tag inSection:0];     [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];     C.主要代码
 1 //
 2 //  Hero.h
 3 //  LOLHero
 4 //
 5 //  Created by hellovoidworld on 14/12/1.
 6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 @interface Hero : NSObject
12 
13 @PRoperty(nonatomic, copy) NSString *icon;
14 @property(nonatomic, copy) NSString *intro;
15 @property(nonatomic, copy) NSString *name;
16 
17 - (instancetype) initWithDictionary:(NSDictionary *) dictionary;
18 + (instancetype) heroWithDictionary:(NSDictionary *) dictionary;
19 + (instancetype) hero;
20 
21 @end
 
 1 //
 2 //  Hero.m
 3 //  LOLHero
 4 //
 5 //  Created by hellovoidworld on 14/12/1.
 6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved.
 7 //
 8 
 9 #import "Hero.h"
10 
11 @implementation Hero
12 
13 - (instancetype) initWithDictionary:(NSDictionary *) dictionary {
14     if (self = [super init]) {
15 //        self.icon = dictionary[@"icon"];
16 //        self.intro = dictionary[@"intro"];
17 //        self.name = dictionary[@"name"];
18         // 代替上方代码, 使用KVC
19         [self setValuesForKeysWithDictionary:dictionary];
20     }
21    
22     return self;
23 }
24 
25 + (instancetype) heroWithDictionary:(NSDictionary *) dictionary {
26     return [[self alloc] initWithDictionary:dictionary];
27 }
28 
29 + (instancetype) hero {
30     return [self heroWithDictionary:nil];
31 }
32 
33 @end
 
  1 //
  2 //  ViewController.m
  3 //  LOLHero
  4 //
  5 //  Created by hellovoidworld on 14/12/1.
  6 //  Copyright (c) 2014年 hellovoidworld. All rights reserved.
  7 //
  8 
  9 #import "ViewController.h"
 10 #import "Hero.h"
 11 
 12 // 遵守了UITableViewDelegate才能响应行点击事件, 遵守UIAlertViewDelegate处理弹窗的事件
 13 @interface ViewController () <UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>
 14 
 15 // UITableView
 16 @property (weak, nonatomic) IBOutlet UITableView *tableView;
 17 
 18 // 所有hero资料
 19 @property(nonatomic, strong) NSArray *heros;
 20 
 21 @end
 22 
 23 @implementation ViewController
 24 
 25 - (void)viewDidLoad {
 26     [super viewDidLoad];
 27     // Do any additional setup after loading the view, typically from a nib.
 28    
 29     // 设置dataSource
 30     self.tableView.dataSource = self;
 31    
 32     // 设置行高
 33     self.tableView.rowHeight = 60;
 34    
 35     // 设置delegate
 36     self.tableView.delegate = self;
 37 }
 38 
 39 - (void)didReceiveMemoryWarning {
 40     [super didReceiveMemoryWarning];
 41     // Dispose of any resources that can be recreated.
 42 }
 43 
 44 /** 隐藏状态栏 */
 45 - (BOOL)prefersstatusBarHidden {
 46     return YES;
 47 }
 48 
 49 /** 延迟加载hero数据 */
 50 - (NSArray *) heros {
 51     if (nil == _heros) {
 52         NSArray *dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil]];
 53        
 54         NSMutableArray *herosArray = [NSMutableArray array];
 55         for (NSDictionary *dict in dictArray) {
 56             Hero *hero = [Hero heroWithDictionary:dict];
 57             [herosArray addObject:hero];
 58         }
 59        
 60         _heros = herosArray;
 61     }
 62    
 63     return _heros;
 64 }
 65 
 66 #pragma mark - 列表方法
 67 
 68 // section数, 默认是1
 69 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 70     return 1;
 71 }
 72 
 73 // 特定section的行数
 74 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
 75     return self.heros.count;
 76 }
 77 
 78 
 79 // 特定行的内容
 80 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 81     Hero *hero = self.heros[indexPath.row];
 82    
 83     // 使用static修饰局部变量,能保证只分配一次存储空间
 84     static NSString *hereCellId = @"hero";
 85    
 86     // 使用特定标识符, 从缓存池查看时候有适合的cell存在
 87     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:hereCellId];
 88    
 89     if (nil == cell) {
 90         // 创建的时候指定标识符
 91         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:hereCellId];
 92     }
 93 
 94     // 标题
 95     cell.textLabel.text = hero.name;
 96    
 97     // 副标题
 98     cell.detailTextLabel.text = hero.intro;
 99    
100     // 图标
101     cell.imageView.image = [UIImage imageNamed:hero.icon];
102 
103     return cell;
104 }
105 
106 #pragma mark - 响应事件
107 
108 /** 点击选中某行 */
109 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
110     // 取得model
111     Hero *hero = self.heros[indexPath.row];
112    
113     // 弹窗
114     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"修改英雄名称" message:nil delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
115    
116     // 设置delegate,也可以在init方法中指定
117     alertView.delegate = self;
118    
119     // 设置弹窗样式
120     alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
121    
122     // 设置弹窗输入框内容
123     [alertView textFieldAtIndex:0].text = hero.name;
124    
125     // 保存index
126     alertView.tag = indexPath.row;
127    
128     // 显示弹窗
129     [alertView show];
130 }
131 
132 /** 处理弹窗点击“确定”按钮事件 */
133 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
134     // 取消
135     if (0 == buttonIndex) return;
136    
137     // 确定
138     // 保存新的名字到model
139     Hero *hero = self.heros[alertView.tag];
140     hero.name = [alertView textFieldAtIndex:0].text;
141    
142     // 刷新数据
143     // 1.全局刷新
144 //    [self.tableView reloadData];
145    
146     // 2.局部刷新
147     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:alertView.tag inSection:0];
148     [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
149    
150 }
151 
152 @end