zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

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

ios数据UI开发 实现 控件 刷新 UITableView
2023-09-14 08:57:58 时间

一、项目文件结构和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.alertView >

复制代码

四、把常用的代码封装成一个带参数的宏

封装方法和代码:

复制代码
 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 }\

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


阿里云EMAS-专家测试服务iOS和Android上百种机型性能、兼容及UI等测试 阿里云EMAS测试专家有着集团内部多个日活过亿规模APP经验,提供EMAS专家测试,客户只需提交测试需求,从用例设计、脚本录制、海量机型测试、整理测试结果、48小时输出专家测试报告均由阿里云EMAS测试专家一站式服务完成。覆盖功能测试、深度兼容测试、性能测试、UI适配测试以及隐私合规检测等,帮助用户以更低成本获得高质量的全面测试能力,可用于APP正式发版前验收,规避手机APP上线前或发版过程中各类隐患。
iOS UI 自动化测试原理以及在 Trip.com 的应用实践 笔者入职 Trip.com 已满一年,回顾这一年的工作历程,约一半的时间都在做 UI 自动化测试相关内容。从而,笔者更深入地研究了 iOS 平台下的自动化测试技术,目前也在负责部门 App 自动化测试平台的搭建和维护。故想借这篇文章一并将所踩过的坑以及学习到的技术,系统且全面地整理出分享给大家。
flutter 在windows和linux上运行IOS UI模拟器 之前发视频总是有人留言,我用的什么模拟器,今天给大家说一下 我一般用的是device_preview这个插件,这个插件的闲置是只能做UI上的模拟,并没有真正的运行环境。 近似您的应用程序在另一台设备上的外观和性能。
如何优化iOS系统上的图文评论UI界面 在我们的社交 APP 上,⽤户的动态由精美的照⽚ 、视频和⽂字组成。对于每张照⽚和视频, 我 们都会展示出完整的标题和五个最新评论。由于⽤户喜欢使⽤标题来讲述照⽚背后的故事, 因此它们通常很⻓ 、很复杂, 并且可能包含超链 接和表情符号。渲染如此复杂的⽂本带来了⼀些问题, 它在滚动时造成性能下降。 即使在 iPhone 12 这样的新设备上, 复杂标题的初始⽂本绘制需要⻓达 50 毫秒, ⽽⽂本展示 需要⻓达 30 毫秒, 渲染速度很慢。⽂本问题还是简单问题, 有时我们需要加载更加复杂的图⽚甚⾄视频。所有这些步骤都发⽣在 UI 线程上, 导致app在⽤户滚动时丢帧。
在 iOS 上面开发界面,需要创建视图、配置界面、视图分层等等很多步骤,也就不可避免的需要书写 N 多的代码。这还仅仅是界面设计,除此之外,完成 controllers 的回调、控制内部事务在界面上的显示效果、界面的操控和内部事务的联系等等多方面的事情都需要手动解决。