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

添加100个数据到UITableView中

     for (int i =  ; i <  ; i ++)
{
NSString *icon = [NSString stringWithFormat:@"00%d.png",arc4random_uniform() + ];
NSString *name = [NSString stringWithFormat:@"第%d",i];
NSString *desc = [NSString stringWithFormat:@"第%d行的描述",i];
Shop *tmp = [Shop shopWithIcon:icon andName:name andDesc:desc];
[_shops addObject:tmp]; }

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

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

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

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

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

看运行结果

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

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

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

选中某行和取消选中某行

  2.1、选中某行执行方法

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

  2. 2、取消选中某行

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

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

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

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

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

 - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. // 读取*.plist文件
// 1.获取全路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"shops" ofType:@"plist"];
// 2.读取数据到数组
NSArray *array = [NSArray arrayWithContentsOfFile:path];
// 初始化数组
_shops = [NSMutableArray array];
_deleteShops = [NSMutableArray array];
//NSLog(@"%d",array.count);
// 添加数据到界面
for (NSDictionary *arr in array)
{
// 1.创建shop
Shop *s = [Shop shopWithDict:arr];
// 2.添加到数组
[_shops addObject:s];
} }

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

 - (id)initWithDict:(NSDictionary *)dict
{
Shop *shop = [[Shop alloc] init];
shop.icon = dict[@"icon"];
shop.name = dict[@"name"];
shop.desc = dict[@"desc"];
return shop;
}
+ (id)shopWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}

效果如图:

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

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

  1. iOS开发学习笔记:基础篇

    iOS开发需要一台Mac电脑.Xcode以及iOS SDK.因为苹果设备都具有自己封闭的环境,所以iOS程序的开发必须在Mac设备上完成(当然,黑苹果应该也是可以的,但就需要花很多的精力去折腾基础环境 ...

  2. ios开发学习笔记(1)

    objective-c基础总结 第一二章 1.application:didiFinishLauchingWithOptions:程序启动后立即执行 2.启动界面代码格式:self.window = ...

  3. iOS开发学习笔记

    1 常用的第三方工具 1.1 iPhone Simulator 测试程序需要模拟器iPhone Simulator 1.2 设计界面需要Interface Builder,Interface Buil ...

  4. ios开发学习笔记(这里一定有你想要的东西,全部免费)

    1,Search Bar 怎样去掉背景的颜色(storyboard里只能设置background颜色,可是发现clear Color无法使用). 其实在代码里还是可以设置的,那就是删除背景view [ ...

  5. IOS开发学习笔记027-UITableView 使用模型对象

    1.模型对象 2.单组数据的显示 1.模型对象 继续优化上一个程序 上一次用到字典,但是坏处多多.这里将这些数据封装到类中. 这就是MVC中得模型,模型就是数据的显示结构 新建一个类,添加几个属性和一 ...

  6. IOS开发学习笔记026-UITableView的使用

    UITableView的简单使用过程 简单介绍 两种样式 UITableViewStylePlain UITableViewStyleGrouped 数据显示需要设置数据源,数据源是符合遵守协议 &l ...

  7. IOS开发学习笔记017-第一个IOS应用

    第一个IOS应用程序,就从最简单的开始吧. 1.先了解一下开发环境,Xcode的相关组成 2.还有模拟器 3.运行与停止按钮 4.新建一个工程 5.看看main函数里都有啥 6.现在来添加一个控件 1 ...

  8. (ios开发学习笔记一)ios项目文件结构

    转自:http://www.cnblogs.com/macroxu-1982/archive/2012/07/31/2616389.html 下面是单个窗体项目例子,我们从这个项目开始,说明ios项目 ...

  9. IOS开发学习笔记043-QQ聊天界面实现

    QQ聊天界面实现 效果如下: 实现过程: 1.首先实现基本界面 头像使用 UIImageView : 文字消息使用 UIButton 标签使用 UILable :水平居中 所有元素在一个cell中,在 ...

随机推荐

  1. CST,CET,UTC,GMT,DST,Unix时间戳几种常见时间概述与关系(转)

    转自:http://www.cnblogs.com/frontendBY/p/5215785.html 1.UTC: Universal Time Coordinated 协调世界时,又称世界标准时间 ...

  2. JavaScript and Ruby in ABAP

    Netweaver里有个mini JavaScript engine CL_JAVA_SCRIPT, 对于Js code的编译和执行都是用system call完成. 只能当玩具用:report SJ ...

  3. hdu-2256 Problem of Precision---矩阵快速幂+数学技巧

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2256 题目大意: 题目要求的是(sqrt(2)+sqrt(3))^2n %1024向下取整的值 解题 ...

  4. python_62_装饰器5

    import time def timer(func): #timer(test1) func=test1 def deco(*args,**kwargs): start_time=time.time ...

  5. 优化通过redis实现的一个抢红包流程【下】

    上一篇文章通过redis实现的抢红包通过测试发现有严重的阻塞的问题,抢到红包的用户很快就能得到反馈,不能抢到红包的用户很久(10秒以上)都无法获得抢红包结果,起主要原因是: 1.用了分布式锁,导致所有 ...

  6. java Socket 客户端服务端对接正确写法(BIO)

    之前在工作中写过一些Socket客户端与服务端的代码,但是当时没有时间仔细研究,只能不报错先过的态度,对其细节了解不深,写的代码有各种问题也浑然不知,只是业务量级以及对接方对接代码没有出现出格的情况所 ...

  7. RxJava2 方法总结

    RxJava2 方法总结 看了许多讲解RxJava的文章,有些文章讲解的内容是基于第一个版本的,有些文章的讲解是通过比较常用的一些API和基础的概念进行讲解的. 但是每次看到RxJava的类中的几十个 ...

  8. Wordpress菜单函数wp_nav_menu各参数详解及示例

    Wordpress菜单函数wp_nav_menu各参数详解及示例   注册菜单 首先要注册菜单,将以下函数添加至function.php函数里   register_nav_menus(array( ...

  9. RuPengGame游戏引擎 精灵 createSprite 创建 setSpritePosition 设置位置 playSpriteAnimate 播放动画 setSpriteFlipX设置翻转 精灵图片下载地址

    package com.swift; import java.awt.Point; import com.rupeng.game.GameCore;//导入游戏引擎包 public class Gam ...

  10. java链接MySQL数据库时使用com.mysql.jdbc.Connection的包会出红线问题 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver问题

    package com.swift; //这里导入的包是java.sql.Connection而不是com.mysql.jdbc.Connection import java.sql.Connecti ...