重用池

作者: 小小东 | 来源:发表于2015-09-21 15:38 被阅读128次

网站:

http://div.gxzj.com.cn/News.aspx?id=225626

下面是转载:

常规配置如下 当超过tableView显示的范围的时候 后面显示的内容将会和前面重复 // 这样配置的话超过页面显示的内容会重复出现 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 定义唯一标识 static NSString *CellIdentifier = @"Cell"; // 通过唯一标识创建cell实例 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // 判断为空进行初始化 --(当拉动页面显示超过主页面内容的时候就会重用之前的cell网站div+css,而不会再次初始化) if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } // 对cell 进行简单地数据配置 cell.textLabel.text = @"text"; cell.detailTextLabel.text = @"text"; cell.imageView.image = [UIImage imageNamed:@"4.png"]; return cell; } //通过以下3方案可以解决 方案一  取消cell的重用机制,网站div+css通过indexPath来创建cell 将可以解决重复显示问题 不过这样做相对于大数据来说内存就比较吃紧了

方案二  让每个cell都拥有一个对应的标识 这样做也会让cell无法重用 所以也就不会是重复显示了 显示内容比较多时内存占用也是比较多的和方案一类似

// 方案一 通过不让他重用cell 来解决重复显示 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 定义唯一标识 static NSString *CellIdentifier = @"Cell"; // 通过indexPath创建cell实例 每一个cell都是单独的 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; // 判断为空进行初始化 --(当拉动页面显示超过主页面内容的时候就会重用之前的cell,psd切图html而不会再次初始化) if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } // 对cell 进行简单地数据配置 cell.textLabel.text = @"text"; cell.detailTextLabel.text = @"text"; cell.imageView.image = [UIImage imageNamed:@"4.png"]; return cell; }

// 方案二 同样通过不让他重用cell 来解决重复显示 不同的是每个cell对应一个标识 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 定义cell标识 每个cell对应一个自己的标识 NSString *CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld",团队网页接活indexPath.section,web外包indexPath.row]; // 通过不同标识创建cell实例 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // 判断为空进行初始化 --(当拉动页面显示超过主页面内容的时候就会重用之前的cell,wap前端外包而不会再次初始化) if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } // 对cell 进行简单地数据配置 cell.textLabel.text = @"text"; cell.detailTextLabel.text = @"text"; cell.imageView.image = [UIImage imageNamed:@"4.png"]; return cell; }

方案三 只要最后一个显示的cell内容不为空div+css制作,然后把它的子视图全部删除,div+css制作等同于把这个cell单独出来了 然后跟新数据就可以解决重复显示 // 方案三 当页面拉动需要显示新数据的时候div前端切图,把最后一个cell进行删除 就有可以自定义cell 此方案即可避免重复显示,网页html切图排版又重用了cell相对内存管理来说是最好的方案 前两者相对比较消耗内存 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { // 定义唯一标识 static NSString *CellIdentifier = @"Cell"; // 通过唯一标识创建cell实例 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; // 判断为空进行初始化 --(当拉动页面显示超过主页面内容的时候就会重用之前的cell网站div+css,而不会再次初始化) if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; } else//当页面拉动的时候 当cell存在并且最后一个存在 把它进行删除就出来一个独特的cell我们在进行数据配置即可避免 { while ([cell.contentView.subviews lastObject] != nil) { [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview]; } } // 对cell 进行简单地数据配置 cell.textLabel.text = @"text"; cell.detailTextLabel.text = @"text"; cell.imageView.image = [UIImage imageNamed:@"4.png"]; return cell; } 

相关文章

  • 重用池

    网站: http://div.gxzj.com.cn/News.aspx?id=225626 下面是转载: 常规配...

  • 第一篇:Objective-C 知识回顾的UI视图部分之一

    1.1.UITableView 重用机制 核心知识点 1 -- 重用池 模拟 UITableView 的重用机制,...

  • 线程池

    线程池 项目文件:HelloJava-ThreadPoolExecutorDemo 线程池优点:重用线程,避免创建...

  • UI篇

    1、什么是重用机制?通过identifer作为标识来创建不同的cell,依托重用池来实现cell的重用 2、UI数...

  • 2019-03-28——Java并发 Executor框架 Sc

    ScheduledThreadPoolExecutor继承ThreadPoolExecutor来重用线程池的功能,...

  • Android 线程池原理

    线程池核心类 : ThreadPoolExecutor:提供了一系列参数来配置线程池 线程池优点: 1.重用线程池...

  • UI视图面试小结

    UITableView重用机制,重点“重用池” UITableView的数据源异步操作,可以使用队列方式,如下图:...

  • 重用机制原理

    重用池:2个NSMutableSet的队列,一个等待使用队列,一个使用中的队列。 自定义的重用池使用方法: 自定义...

  • java多线程学习5

    线程池 ThreadPoolExecutor 官方API解释线程池的好处: (1)通过重用线程池中的线程,来减少每...

  • 一篇文章搞懂线程池

    线程池 什么使用使用线程池? 单个任务处理时间比较短 需要处理的任务数量很大 线程池优势 重用存在的线程,减少线程...

网友评论

      本文标题:重用池

      本文链接:https://www.haomeiwen.com/subject/mppbcttx.html