iOS开发UI篇—实现UItableview控件数据刷新

简介:

一、项目文件结构和plist文件

二、实现效果

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

运行界面:

点击选中行:

修改数据后自动刷新:

三、代码示例

数据模型部分:

YYheros.h文件

复制代码
 1 //  2 // YYheros.h
 3 // 10-英雄展示(数据刷新)
 4 //  5 // Created by apple on 14-5-29.
 6 // Copyright (c) 2014年 itcase. All rights reserved.
 7 //
 8  9 #import <Foundation/Foundation.h>
10 #import "Global.h" 11 12 @interface YYheros : NSObject
13 @property(nonatomic,copy)NSString *name;
14 @property(nonatomic,copy)NSString *icon;
15 @property(nonatomic,copy)NSString *intro;
16 17 //-(instancetype)initWithDict:(NSDictionary *)dict;
18 //+(instancetype)herosWithDict:(NSDictionary *)dict; 19 YYinitH(hero)
20 @end
复制代码

YYheros.m文件

复制代码
 1 //  2 // YYheros.m
 3 // 10-英雄展示(数据刷新)
 4 //  5 // Created by apple on 14-5-29.
 6 // Copyright (c) 2014年 itcase. All rights reserved.
 7 //
 8  9 #import "YYheros.h" 10 11 @implementation YYheros
12 //-(instancetype)initWithDict:(NSDictionary *)dict
13 //{
14 // if (self=[super init]) { 15 //// self.name=dict[@"name"];
16 //// self.icon=dict[@"icon"];
17 //// self.intro=dict[@"intro"]; 18 // 19 // //使用KVC
20 // [self setValuesForKeysWithDictionary:dict];
21 // }
22 // return self;
23 //}
24 // 25 //+(instancetype)herosWithDict:(NSDictionary *)dict
26 //{
27 // return [[self alloc]initWithDict:dict];
28 //} 29 YYinitM(hero)
30 @end
复制代码

主控制器 YYViewController.m文件

复制代码
 1 //  2 // YYViewController.m
 3 // 10-英雄展示(数据刷新)
 4 //  5 // Created by apple on 14-5-29.
 6 // Copyright (c) 2014年 itcase. All rights reserved.
 7 //
 8  9 #import "YYViewController.h"  10 #import "YYheros.h"  11  12 @interface YYViewController ()<UITableViewDataSource,UIAlertViewDelegate,UITableViewDelegate>
 13 @property (strong, nonatomic) IBOutlet UITableView *tableview;
 14 @property(nonatomic,strong)NSArray *heros;
 15 @end  16  17 @implementation YYViewController
 18  19 - (void)viewDidLoad
 20 {
 21  [super viewDidLoad];
 22 //设置数据源  23 self.tableview.dataSource=self;
 24 self.tableview.delegate=self;
 25 self.tableview.rowHeight=60.f;
 26 NSLog(@"%d",self.heros.count);
 27 }
 28  29 #pragma mark -懒加载
 30 -(NSArray *)heros
 31 {
 32 if (_heros==nil) {
 33  34 NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"heros.plist" ofType:nil];
 35 NSArray *temparray=[NSArray arrayWithContentsOfFile:fullpath];
 36  37 NSMutableArray *arrayM=[NSMutableArray array];
 38 for (NSDictionary *dict in temparray) {
 39 YYheros *hero=[YYheros herosWithDict:dict];
 40  [arrayM addObject:hero];
 41  }
 42 _heros=[arrayM mutableCopy];
 43  }
 44 return _heros;
 45 }
 46  47 #pragma mark- tableview的处理
 48 //多少组  49 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
 50 {
 51 return 1;
 52 }
 53 //多少行  54 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 55 {
 56 return self.heros.count;
 57 }
 58 //每组每行的数据,设置cell  59 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 60 {
 61 //NSLog(@"cellForRowAtIndexPath 修改的了 %d", indexPath.row);
 62 //1.去缓存中取  63 static NSString *identifier=@"hero";
 64 UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
 65 //2.如果没有,那么就自己创建  66 if (cell==nil) {
 67 NSLog(@"chuangjiancell");
 68 cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
 69  }
 70 //3.设置数据
 71  72 //3.1拿到该行的模型  73 YYheros *hero=self.heros[indexPath.row];
 74 cell.textLabel.text=hero.name;
 75 cell.imageView.image=[UIImage imageNamed:hero.icon];
 76 cell.detailTextLabel.text=hero.intro;
 77 //4.返回cell  78 return cell;
 79 }
 80  81 #pragma mark-数据刷新
 82 //1.弹出窗口,拿到数据
 83 //当某一行被选中的时候调用该方法  84 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 85 {
 86 //拿到改行的数据模型  87 YYheros *hero=self.heros[indexPath.row];
 88 UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"修改数据" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
 89  90 //密码框形式的
 91 //alert.alertViewStyle=UIAlertViewStyleSecureTextInput;  92 alert.alertViewStyle=UIAlertViewStylePlainTextInput;
 93 UITextField *text=[alert textFieldAtIndex:0];
 94 //把当前行的英雄数据显示到文本框中  95 text.text=hero.name;
 96 alert.tag=indexPath.row;
 97  [alert show];
 98 }
 99 //2.修改数据,完成刷新操作 100 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
101 {
102 //1.修改模型
103 //如果选中的是取消,那么就返回,不做任何操作 104 if (0==buttonIndex) return;
105 //否则就修改模型,刷新数据 106 YYheros *hero=self.heros[alertView.tag];
107 108 //拿到当前弹窗中的文本数据(已经修改后的数据) 109 UITextField *text=[alertView textFieldAtIndex:0];
110 //用修改后的数据去修改模型 111 hero.name=text.text;
112 113 //2.刷新数据
114 // 只要调用tableview的该方法就会自动重新调用数据源的所有方法
115 // 会自动调用numberOfSectionsInTableView
116 // 会自动调用numberOfRowsInSection
117 // 会自动调用cellForRowAtIndexPath
118 // [self.tableview reloadData];
119 120 // 刷新指定行 121 NSIndexPath *path = [NSIndexPath indexPathForRow:alertView.tag inSection:0];
122  [self.tableview reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationRight];
123 //如果不进行刷新会怎么样?(修改之后不会即时刷新,要等到重新对cell进行数据填充的时候才会刷新) 124 }
125 //隐藏状态栏 126 -(BOOL)prefersStatusBarHidden
127 {
128 return YES;
129 }
130 @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 YYinitH(name) -(instancetype)initWithDict:(NSDictionary *)dict;\
16 +(instancetype)herosWithDict:(NSDictionary *)dict;
17 18 19 #define YYinitM(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
复制代码

以后在需要使用的时候,只需要使用宏即可。

如在YYheros.m文件中使用YYinitM(hero)这一句代码可以代替下面的代码段:

复制代码
-(instancetype)initWithDict:(NSDictionary *)dict
{
 if (self=[super init]) {
// self.name=dict[@"name"];
// self.icon=dict[@"icon"];
// self.intro=dict[@"intro"];
 
 //使用KVC  [self setValuesForKeysWithDictionary:dict];
 }
 return self;
}

+(instancetype)herosWithDict:(NSDictionary *)dict
{
 return [[self alloc]initWithDict:dict];
}
复制代码

五、注意点

1.刷新数据的两个步骤:

1)修改模型

2)刷新表格数据(可以全部刷新,也可以刷新指定的行)

2.在主控制器文件中,遵守了三个协议

分别是:

UITableViewDataSource,

UIAlertViewDelegate,

UITableViewDelegate

目录
相关文章
|
12月前
|
开发框架 前端开发 Android开发
Flutter 与原生模块(Android 和 iOS)之间的通信机制,包括方法调用、事件传递等,分析了通信的必要性、主要方式、数据传递、性能优化及错误处理,并通过实际案例展示了其应用效果,展望了未来的发展趋势
本文深入探讨了 Flutter 与原生模块(Android 和 iOS)之间的通信机制,包括方法调用、事件传递等,分析了通信的必要性、主要方式、数据传递、性能优化及错误处理,并通过实际案例展示了其应用效果,展望了未来的发展趋势。这对于实现高效的跨平台移动应用开发具有重要指导意义。
1094 4
|
前端开发
Element UI 【实战】纯前端对表格数据进行增删改查(内含弹窗表单、数据校验、时间日期格式)
Element UI 【实战】纯前端对表格数据进行增删改查(内含弹窗表单、数据校验、时间日期格式)
506 6
|
iOS开发 开发者
iOS平台RTMP|RTSP播放器如何实时回调YUV数据
我们在做RTMP、RTSP播放器的时候,有开发者需要自己处理拉取到的YUV数据,做二次分析之用,为此,我们做了以下的设计:InitPlayer之后,再调用SmartPlayerStart()接口之前,设置yuv数据回调即可。
209 6
|
前端开发 JavaScript UED
element-ui 表格数据究竟隐藏着怎样的神秘样式与格式化技巧?快来揭开谜底!
【8月更文挑战第22天】《element-ui 表格数据样式及格式化案例》展示了如何利用 element-ui 的表格组件实现美观且易读的数据展示。通过简单配置,可以自定义表格样式,如边框、背景色等,并通过 formatter 实现数据格式化,例如将成绩保留一位小数。此外,还能依据条件设置行样式,如成绩达优则高亮显示,从而增强用户体验和数据可读性。
257 1
|
测试技术 Swift iOS开发
探索iOS自动化测试:使用Swift编写UI测试
【8月更文挑战第31天】在软件开发的海洋中,自动化测试是保证船只不偏离航线的灯塔。本文将带领读者启航,深入探索iOS应用的自动化UI测试。我们将通过Swift语言,点亮代码的灯塔,照亮测试的道路。文章不仅会展示如何搭建测试环境,还会提供实用的代码示例,让理论知识在实践中生根发芽。无论你是新手还是有经验的开发者,这篇文章都将是你技能提升之旅的宝贵指南。
Element UI【级联选择器】el-cascader 获取选中内容的 label 数据,鼠标悬浮显示超长内容
Element UI【级联选择器】el-cascader 获取选中内容的 label 数据,鼠标悬浮显示超长内容
1660 3
Element UI 多选表格--判断勾选数据行的 Checkbox 时为选中还是取消选中
Element UI 多选表格--判断勾选数据行的 Checkbox 时为选中还是取消选中
481 1
Element UI 多选表格【翻页多选】简易版(不支持翻页多选数据反显)
Element UI 多选表格【翻页多选】简易版(不支持翻页多选数据反显)
311 0
Element UI 多选表格【翻页多选】简易版(不支持翻页多选数据反显)
软件研发核心问题之在需求拆解过程中,“数据与UI如何关联”的问题如何解决
软件研发核心问题之在需求拆解过程中,“数据与UI如何关联”的问题如何解决
137 0
Element UI 多选表格【翻页多选】全能版(含翻页多选数据反显、toggleRowSelection失效的原因解析和解决方案)
Element UI 多选表格【翻页多选】全能版(含翻页多选数据反显、toggleRowSelection失效的原因解析和解决方案)
998 0

热门文章

最新文章