IOS开发学习笔记028-UITableView单组数据显示代码优化

1、性能优化(添加几百个cell到view)

2、如何实现选中某行,改变这个cell最右侧显示的对号按钮

1、如果表格中又几百条数据的话,系统会自动加载显示在界面上得数据,逐一加载

添加100个数据到UITableView中

1     for (int i = 0 ; i < 100 ; i ++)
2     {
3         NSString *icon = [NSString stringWithFormat:@"00%d.png",arc4random_uniform(8) + 1];
4         NSString *name = [NSString stringWithFormat:@"第%d",i];
5         NSString *desc = [NSString stringWithFormat:@"第%d行的描述",i];
6         Shop *tmp = [Shop shopWithIcon:icon andName:name andDesc:desc];
7         [_shops addObject:tmp];
8
9     }

在滑动屏幕进行显示的时候,只会加载当前屏幕中显示的数据。

 1 // 设置行内容
 2 // 每当有一个cell进入视野范围内就会调用
 3 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 4 {
 5      UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"c1"];
 6     cell.textLabel.text = [NSString stringWithFormat:@"第%ld行",indexPath.row];
 7     NSLog(@"%p,第%ld行数据",cell,indexPath.row);
 8
 9     return cell;
10 }

界面中只显示了三个cell,如下图,向下滑动,每次超过三个时就加载新的cell,向上滑动会重新加载cell,而且每次都会重新申请内存.

如果想避免这种情况可以使用缓存池,这是UITableViewCell 自带的方法 dequeueReusableCellWithIdentifier

 1 // 设置行内容
 2 // 每当有一个cell进入视野范围内就会调用
 3 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 4 {
 5      // 从缓存池中选择可循环利用的cell,指定标识c1,这样就会找到结构一样的cell
 6     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"c1"];
 7     // 如果缓存池中没有
 8     if (cell == nil)
 9     {
10         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"c1"]; // 设定标识C1
11     }
12     // UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"c1"];
13     cell.textLabel.text = [NSString stringWithFormat:@"第%ld行",indexPath.row];
14     NSLog(@"%p,第%ld行数据",cell,indexPath.row);
15
16     return cell;
17 }

看运行结果

界面开始显示三个cell,向下滑动时会有一个过渡这回新建一个cell,但是接着往下就会使用已经存在的cell,从第四行开始使用第0行创建的cell

源代码:http://pan.baidu.com/s/1i3qyAjj

2、如何实现选中某行,改变这个cell最右侧显示的对号按钮

选中某行和取消选中某行

1、选中某行执行方法

 1 // 选中某行执行
 2 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 3 {
 4     NSLog(@"selected");
 5     //选中后颜色变深
 6     // 在最右侧显示一个对号图标
 7     // 1、获得选中行
 8     Shop *s = _shops[indexPath.row];
 9     // 2、修改选中行的数据,将选中的cell添加到待删除数组中
10     if ([_deleteShops containsObject:s]) // 如果已经存在,再次点击就取消选中按钮
11     {
12         [_deleteShops removeObject:s];
13     }
14     else    // 否则就添加待删除数组
15     {
16         [_deleteShops addObject:s];
17     }
18     // 3、更新数据,更新数据也就是重新设置某一行的内容
19     [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
20
21 }

2、取消选中某行

1 // 取消选中某行执行
2 - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
3 {
4     NSLog(@"Deselected");
5 }

3、重新设置选中行的内容

 1 // 设置行内容
 2 // 每当有一个cell进入视野范围内就会调用
 3 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 4 {
 5     static NSString *ID = @"C1";
 6     // 从缓存池中选择可循环利用的cell,指定标识c1,这样就会找到结构一样的cell
 7     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
 8     // 如果缓存池中没有
 9     if (cell == nil)
10     {
11         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; // 设定标识C1
12     }
13     // UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"c1"];
14     // 更新数据到界面
15     Shop *s = _shops[indexPath.row];
16     cell.textLabel.text = s.name;
17     cell.imageView.image = [UIImage imageNamed:s.icon];;
18     cell.detailTextLabel.text = s.desc;
19     // 显示最右侧的按钮
20     if ([_deleteShops containsObject:s]) // 判断是否已经选中的cell,是得话设置图标
21     {
22         cell.accessoryType = UITableViewCellAccessoryCheckmark;
23     }
24     else    // 否则就什么都不显示
25     {
26         cell.accessoryType = UITableViewCellAccessoryNone;
27     }
28
29    // NSLog(@"%p,第%ld行数据",cell,indexPath.row);
30
31     return cell;
32 }

代码中使用一个新的数组来保存选中的行_deleteShops,并在更新数据事进行判断。

4、加载图片和文字使用一个plist文件

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     // Do any additional setup after loading the view, typically from a nib.
 5
 6     // 读取*.plist文件
 7     // 1.获取全路径
 8     NSString *path = [[NSBundle mainBundle] pathForResource:@"shops" ofType:@"plist"];
 9     // 2.读取数据到数组
10     NSArray *array = [NSArray arrayWithContentsOfFile:path];
11     // 初始化数组
12     _shops  = [NSMutableArray array];
13     _deleteShops = [NSMutableArray array];
14     //NSLog(@"%d",array.count);
15     // 添加数据到界面
16     for (NSDictionary *arr in array)
17     {
18         // 1.创建shop
19         Shop *s = [Shop shopWithDict:arr];
20         // 2.添加到数组
21         [_shops addObject:s];
22     }
23
24 }

5、shop模型进行了其他一些修改,增减一个类方法和一个对象方法用于返回Shop对象

 1 - (id)initWithDict:(NSDictionary *)dict
 2 {
 3     Shop *shop = [[Shop alloc] init];
 4     shop.icon = dict[@"icon"];
 5     shop.name = dict[@"name"];
 6     shop.desc = dict[@"desc"];
 7     return shop;
 8 }
 9 + (id)shopWithDict:(NSDictionary *)dict
10 {
11     return [[self alloc] initWithDict:dict];
12 }

效果如图:

源代码: http://pan.baidu.com/s/1mgxKgMO

时间: 2024-08-06 03:44:43

IOS开发学习笔记028-UITableView单组数据显示代码优化的相关文章

IOS开发学习笔记-(2)键盘控制,键盘类型设置,alert 对话框

一.关闭键盘,放弃第一响应者,处理思路有两种 ① 使用文本框的 Did End on Exit 绑定事件 ② UIControl on Touch 事件 都去操作 sender 的  resignFirstResponder #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UITextField *txtUserName; @pro

iOS开发学习笔记:基础篇

iOS开发需要一台Mac电脑.Xcode以及iOS SDK.因为苹果设备都具有自己封闭的环境,所以iOS程序的开发必须在Mac设备上完成(当然,黑苹果应该也是可以的,但就需要花很多的精力去折腾基础环境),Xcode是一个集成开发环境,包括了编辑器.调试.模拟器等等一系列方便开发和部署的工具,iOS SDK则是开发应用所必需,不同的SDK分别对应不同的iOS版本或设备,通常我们需要下载多个iOS SDK以确保我们开发的程序能够在不同版本的iOS上正常运行. 创建新工程 Xcode提供了很多种工程模

IOS开发学习笔记-(3) 进度条、等待动画开始停止

一.创建对应空间视图  ,如下图: 二.编写对应的 .h 代码,如下 : #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activWaitNetWork; @property (weak, nonatomic) IBOutlet UIProgressView *pgrsDownLo

IOS开发学习笔记(二)-语音识别(科大讯飞)

上次简单地讲解了如何利用科大讯飞完成语音合成,今天接着也把语音识别整理一下.当然,写代码前我们需要做的一些工作(如申请appid.导库),在上一篇语音合成的文章当中已经说过了,不了解的可以看看我上次的博文,那么这次直接从堆代码开始吧. 详细步骤: 1.导完类库之后,在工程里添加好用的头文件.在视图里只用了一个UITextField显示识别的内容,两个UIButton(一个开始监听语音,一个结束监听),然后引入类.添加代理,和语音合成的一样. MainViewController.h 1 #imp

IOS开发学习笔记(1)-----UILabel 详解

1. [代码][C/C++]代码     //创建uilabelUILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, 280, 80)];//设置背景色label1.backgroundColor = [UIColor grayColor];//设置taglabel1.tag = 91;//设置标签文本label1.text = @"Hello world!";//设置标签文本字体和字体大小label1.

IOS开发学习笔记(2)-----UIButton 详解

1. [代码][C/C++]代码     //这里创建一个圆角矩形的按钮    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];    //    能够定义的button类型有以下6种,//    typedef enum {//        UIButtonTypeCustom = 0,          自定义风格//        UIButtonTypeRoundedRect,        

IOS开发学习笔记--语音合成(科大讯飞)

      现在语音服务越来越热,我们平时使用的很多软件都带有语音合成和识别功能,用起来也很方便.说到语音服务,Google和微软都提供过API接口,不过笔者要介绍的是国内的智能语音技术提供商---科大讯飞.之前看过一个比较Google.微软和科大讯飞语音识别引擎的博文(http://fqctyj.blog.163.com/blog/static/70843455201361955322797/),有兴趣可以去看看.笔者接触语音服务的时间也不长,对语音服务也不是很了解,但是拆解过科大讯飞的Dem

IOS开发学习笔记017-什么是IOS开发

应用程序开发流程 1.IOS开发需要思考的问题 用户是谁?不同应用程序的内容和用户体验大不相同,这取决于想要编写的是什么应用程序,它可能是儿童游戏,也可能是待办事项列表应用程序,又或者是测试自己学习成果的应用程序. 应用程序的用途是什么?赋予应用程序一个明确的用途十分重要.了解激发用户使用应用程序的动因是界定用途的一个出发点. 应用程序尝试解决什么问题?应用程序应该完美解决单个问题,而不是尝试解决多个截然不同的问题.如果发现应用程序尝试解决不相关的问题,那么最好考虑编写多个应用程序. 应用程序要

ios开发学习笔记(1)

objective-c基础总结 第一二章 1.application:didiFinishLauchingWithOptions:程序启动后立即执行 2.启动界面代码格式:self.window = [UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];//1.从Infor.plist中取出版本号NString *version = [NSBundle mainBundle].infoDictionary[key];//2.