iOS开发UI篇—实现UItableview控件数据刷新
iOS开发UI篇—实现UItableview控件数据刷新
一、项目文件结构和plist文件

二、实现效果
1.说明:这是一个英雄展示界面,点击选中行,可以修改改行英雄的名称(完成数据刷新的操作).
运行界面:

点击选中行:

修改数据后自动刷新:

三、代码示例
数据模型部分:
TXHero.h文件
1 #import <Foundation/Foundation.h> 2 3 @interface TXHero : NSObject 4 @property (nonatomic, copy) NSString *name; 5 @property (nonatomic, copy) NSString *icon; 6 @property (nonatomic, copy) NSString *intro; 7 8 + (instancetype)heroWithDict:(NSDictionary *)dict; 9 - (instancetype)initWithDict:(NSDictionary *)dict; 10 @end
TXHero.m文件
1 #import "TXHero.h" 2 3 @implementation TXHero 4 + (instancetype)heroWithDict:(NSDictionary *)dict 5 { 6 return [[self alloc] initWithDict:dict]; 7 } 8 9 - (instancetype)initWithDict:(NSDictionary *)dict 10 { 11 if (self = [super init]) { 12 [self setValuesForKeysWithDictionary:dict]; 13 } 14 return self; 15 } 16 @end
主控制器 TXViewController.m文件
1 #import "TXViewController.h" 2 #import "TXHero.h" 3 4 @interface TXViewController () <UITableViewDataSource, UITableViewDelegate , UIAlertViewDelegate> 5 @property (nonatomic, strong) NSArray *heros; 6 @property (weak, nonatomic) IBOutlet UITableView *tableView; 7 8 @end 9 10 @implementation TXViewController 11 12 - (void)viewDidLoad 13 { 14 [super viewDidLoad]; 15 16 // 设置行高(每一行的高度一致) 17 // self.tableView.rowHeight = 60; 18 19 // self.tableView.delegate = self; 20 } 21 22 - (NSArray *)heros 23 { 24 if (_heros == nil) { 25 // 初始化 26 // 1.获得plist的全路径 27 NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil]; 28 29 // 2.加载数组 30 NSArray *dictArray = [NSArray arrayWithContentsOfFile:path]; 31 32 // 3.将dictArray里面的所有字典转成模型对象,放到新的数组中 33 NSMutableArray *heroArray = [NSMutableArray array]; 34 for (NSDictionary *dict in dictArray) { 35 // 3.1.创建模型对象 36 TXHero *hero = [TXHero heroWithDict:dict]; 37 38 // 3.2.添加模型对象到数组中 39 [heroArray addObject:hero]; 40 } 41 42 // 4.赋值 43 _heros = heroArray; 44 } 45 return _heros; 46 } 47 48 #pragma mark - 数据源方法 49 // 不实现这个方法,默认就是1组 50 //- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 51 //{ 52 // return 1; 53 //} 54 55 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 56 { 57 return self.heros.count; 58 } 59 60 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 61 { 62 UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]; 63 64 // 取出模型 65 TXHero *hero = self.heros[indexPath.row]; 66 67 // 设置cell的数据 68 cell.textLabel.text = hero.name; 69 cell.detailTextLabel.text = hero.intro; 70 cell.imageView.image = [UIImage imageNamed:hero.icon]; 71 72 return cell; 73 } 74 75 #pragma mark - 代理方法 76 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 77 { 78 //1.取得被点击这行对应的模型 79 TXHero *hero = self.heros[indexPath.row]; 80 81 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"数据展示" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; 82 83 //设置对话框类型 84 85 alert.alertViewStyle = UIAlertViewStylePlainTextInput; 86 //取得唯一的那个文本框,显示英雄的名称 87 [alert textFieldAtIndex:0].text = hero.name; 88 89 [alert show]; 90 91 //绑定行号到alertview上 92 alert.tag = indexPath.row; 93 94 95 96 } 97 #pragma mark - alertView的代理方法 98 /** 99 * 点击了alertView上面的按钮就会调用这个方法 100 * 101 * @param buttonIndex 按钮的索引,从0开始 102 */ 103 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 104 { 105 if (buttonIndex == 0) return; 106 107 // 按钮的索引肯定不是0 108 109 // 1.取得文本框最后的文字 110 NSString *name = [alertView textFieldAtIndex:0].text; 111 // int row = alertView.tag; 112 // NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0]; 113 // UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:path]; 114 // cell.textLabel.text = name; 115 116 // 2.修改模型数据 117 int row = alertView.tag; 118 TXHero *hero = self.heros[row]; 119 hero.name = name; 120 121 // 3.告诉tableView重新加载模型数据 122 // reloadData : tableView会向数据源重新请求数据 123 // 重新调用数据源的相应方法取得数据 124 // 重新调用数据源的tableView:numberOfRowsInSection:获得行数 125 // 重新调用数据源的tableView:cellForRowAtIndexPath:得知每一行显示怎样的cell 126 // 全部刷新 127 // [self.tableView reloadData]; 128 129 // 局部刷新 130 NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0]; 131 [self.tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationBottom]; 132 133 134 135 136 137 } 138 /** 139 * 每一行的高度不一致的时候使用这个方法来设置行高 140 */ 141 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 142 { 143 if (indexPath.row == 0) return 100; 144 return 60; 145 } 146 147 @end
四、把常用的代码封装成一个带参数的宏
封装方法和代码:
1 // 2 // Global.h 3 // 10-英雄展示(数据刷新) 4 // 5 // Created by apple on 14-5-29. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #ifndef _0____________Global_h 10 #define _0____________Global_h 11 12 /** 13 * 自定义带参数的宏 14 */ 15 #define TXinitH(name) -(instancetype)initWithDict:(NSDictionary *)dict;\ 16 +(instancetype)herosWithDict:(NSDictionary *)dict; 17 18 19 #define TXinitM(name) -(instancetype)initWithDict:(NSDictionary *)dict\ 20 {\ 21 if (self=[super init]) {\ 22 [self setValuesForKeysWithDictionary:dict];\ 23 }\ 24 return self;\ 25 }\ 26 \ 27 +(instancetype)herosWithDict:(NSDictionary *)dict\ 28 {\ 29 return [[self alloc]initWithDict:dict];\ 30 }\ 31 32 #endif
以后在需要使用的时候,只需要使用宏即可。
如在TXheros.m文件中使用TXinitM(hero)这一句代码可以代替下面的代码段:
1 -(instancetype)initWithDict:(NSDictionary *)dict 2 { 3 if (self=[super init]) { 4 // self.name=dict[@"name"]; 5 // self.icon=dict[@"icon"]; 6 // self.intro=dict[@"intro"]; 7 8 //使用KVC 9 [self setValuesForKeysWithDictionary:dict]; 10 } 11 return self; 12 } 13 14 +(instancetype)herosWithDict:(NSDictionary *)dict 15 { 16 return [[self alloc]initWithDict:dict]; 17 }
五、注意点
1.刷新数据的两个步骤:
1)修改模型
2)刷新表格数据(可以全部刷新,也可以刷新指定的行)
2.在主控制器文件中,遵守了三个协议
分别是:
UITableViewDataSource,
UIAlertViewDelegate,
UITableViewDelegate
浙公网安备 33010602011771号