cell的循环利用——实现游戏英雄展示界面
2013-11-14 15:18
113 查看
1.实例一:用cell实现英雄展示
详见: http://blog.csdn.net/sunnyboy9/article/details/161167572.实例二:循环利用cell实现相同功能
代码:// // MJViewController.m #import "MJViewController.h" #import "MJHero.h" @interface MJViewController () <UITableViewDataSource> @property (nonatomic, strong) NSArray *heros; @property (weak, nonatomic) IBOutlet UITableView *tableView; @end @implementation MJViewController - (void)viewDidLoad { [super viewDidLoad]; // 设置行高(每一行的高度一致) self.tableView.rowHeight = 60; } - (NSArray *)heros { if (_heros == nil) { // 初始化 // 1.获得plist的全路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"heros.plist" ofType:nil]; // 2.加载数组 NSArray *dictArray = [NSArray arrayWithContentsOfFile:path]; // 3.将dictArray里面的所有字典转成模型对象,放到新的数组中 NSMutableArray *heroArray = [NSMutableArray array]; for (NSDictionary *dict in dictArray) { // 3.1.创建模型对象 MJHero *hero = [MJHero heroWithDict:dict]; // 3.2.添加模型对象到数组中 [heroArray addObject:hero]; } // 4.赋值 _heros = heroArray; } return _heros; } - (BOOL)prefersStatusBarHidden { return YES; } #pragma mark - 数据源方法 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.heros.count; } // 离开屏幕的cell会怎样 /** * 每当有一个cell进入视野范围内,就会调用 */ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // static修饰局部变量:可以保证局部变量只分配一次存储空间(只初始化一次) static NSString *ID = @"hero"; // 1.通过一个标识去缓存池中寻找可循环利用的cell // dequeue : 出列 (查找) UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID]; // 2.如果没有可循环利用的cell if (cell == nil){ cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID]; // NSLog(@"------缓存池找不到cell--%d", indexPath.row); } // 3.给cell设置新的数据 // 取出模型(某一行) MJHero *hero = self.heros[indexPath.row]; // 设置cell的数据 cell.textLabel.text = hero.name; cell.detailTextLabel.text = hero.intro; cell.imageView.image = [UIImage imageNamed:hero.icon]; return cell; } @end
运行结果
3.总结:循环利用的优点
实例一:创建cell的时候,用户能看到多少个就创建多少个,当拖动的时候又创建 就是当用到的时候就创建,也就是当用户拖动界面的时候就会无限创建,这样性能就很低,内存空间伤不起,所以采用循环利用cell的方法,实例二通过缓存池实现循环利用,当要用的时候就到缓存池里去寻找,没有时才创建,而当离开屏幕的cell就被销毁掉,这样性能会有很大提升。4.Cell的重用原理和重用步骤
4.1重用原理:
iOS设备的内存有限,如果用UITableView显示成千上万条数据,就需要成千上万个UITableViewCell对象的话,那将会耗尽iOS设备的内存。要解决该问题,需要重用UITableViewCell对象当滚动列表时,部分UITableViewCell会移出窗口,UITableView会将窗口外的UITableViewCell放入一个对象池中,等待重用。当UITableView要求dataSource返回UITableViewCell时,dataSource会先查看这个对象池,如果池中有未使用的UITableViewCell,dataSource会用新的数据配置这个UITableViewCell,然后返回给UITableView,重新显示到窗口中,从而避免创建新对象
4.2Cell的重用代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// 1.定义一个cell的标识
static NSString *ID = @"mjcell";
// 2.从缓存池中取出cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
// 3.如果缓存池中没有cell
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
}
// 4.设置cell的属性...
return cell;
相关文章推荐
- UITableViewCell 单组数据展示——实现游戏英雄单组展示
- 跑酷类游戏------quick-cocos2d-x 加载英雄与其动画到界面具体实现(一)
- 用Ogre MyGUI实现一套完整的游戏界面及实现方法介绍
- 我的Android进阶之旅------>Android疯狂连连看游戏的实现之加载界面图片和实现游戏Activity(四)
- 基于QML Pathview的大型游戏英雄联盟-预览界面
- 用Ogre MyGUI实现一套完整的游戏界面及实现方法介绍
- iOS 实现多个可变 cell 复杂界面的制作
- JAVA实现大华摄像头WEB方式实时显示视频,H5界面展示方式思路。
- 在uiview 的tableView中点击cell进入跳转到另一个界面的实现方法
- 自定义摄像机应用实现之预览界面展示
- 利用SDL+CEGUI框架实现VTP的游戏界面风格
- [cocos2d-x][游戏开发]通过cocos2d-x实现简易飞机大战 03.游戏界面 背景层实现滚动
- [cocos2d-x][游戏开发]通过cocos2d-x实现简易飞机大战 07.游戏界面 敌机的产生与移动
- 一、实现欢迎界面和游戏背景图的滚动(雷霆战机)
- 安卓第二课:教材中游戏启动界面实现之我有疑问
- 用Ogre MyGUI实现一套完整的游戏界面及实现方法介绍
- linux下使用C语言实现2048简易字符界面游戏
- cocos2d-x 实现按Home和待机键后,再进入游戏显示暂停界面
- [cocos2d-x][游戏开发]通过cocos2d-x实现简易飞机大战 08.游戏界面 碰撞检测
- 【Android游戏开发之五】游戏注册界面Demo-实现两个Activity之间的切换与数据交互!