前言

这里说的内容复用,是指添加到 ScrollView 里面的试图是同一个模型;比如,我需要在 ScrollView 上添加100个 xkView(其他封装好的VC、UIView),每次滑动 ScrollView 时,只需要更新 xkView 上的内容就行;ScrollView内容较多的情况下,可以考虑复用。

最近做试卷排版,在做试卷展示时,我封装好了一个基于VC的试题模型 PaperQuestionViewController(用于显示每道试题的内容,模板里要加 index 索引属性,便于复用),因为一套试卷,会有100+ 道试题,因为我的排版用到了 Coretext ,如果一下子把100+ 个试图同时添加到ScrollView上,不复用,内存会比较大,这是复用最重要的原因;【也可以用UIcollectionView,根据需求而定】。

实现

当前VC.m

///所有试题数组
@property (nonatomic,strong) NSArray *arrayQuestin; ///UIScrollView
@property (nonatomic,strong) UIScrollView *scrollview; ///保存可见的视图
@property (nonatomic, strong) NSMutableSet *visibleViewControllers; /// 保存可重用的
@property (nonatomic, strong) NSMutableSet *reusedViewControllers;

引用 ScrollView 代理

<UIScrollViewDelegate>

实现代理方法

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
///更新模板信息
[self showVc];
}

附加方法

///显示试图
- (void)showVc{
// 获取当前处于显示范围的 控制器 索引
CGRect visibleBounds = self.scrollview.bounds;
CGFloat minX = CGRectGetMinX(visibleBounds);
CGFloat maxX = CGRectGetMaxX(visibleBounds);
CGFloat width = CGRectGetWidth(visibleBounds);
NSInteger firstIndex = (NSInteger)floorf(minX / width);
NSInteger lastIndex = (NSInteger)floorf(maxX / width); // 处理越界
if (firstIndex < ) {
firstIndex = ;
}
if (lastIndex >= self.arrayQuestin.count) {
lastIndex = (self.arrayQuestin.count - );
}
// 回收掉不在显示的
NSInteger viewIndex = ;
for (PaperQuestionViewController * vc in self.visibleViewControllers) {
viewIndex = vc.index;
// 不在显示范围内
if ( viewIndex < firstIndex || viewIndex > lastIndex) {
[self.reusedViewControllers addObject:vc];
[vc removeFromParentViewController];
[vc.view removeFromSuperview];
}
}
[self.visibleViewControllers minusSet:self.reusedViewControllers];
// 是否需要显示新的视图
for (NSInteger index = firstIndex; index <= lastIndex; index ++) {
BOOL isShow = NO;
for (BookPaperQuestionViewController * childVc in self.visibleViewControllers) { if (childVc.index == index) {
isShow = YES;
}
}
if (!isShow ) {
[self showVcWithIndex:index];
}
}
} // 显示一个 view
- (void)showVcWithIndex:(NSInteger)index{
PaperQuestionViewController *vc = [self.reusedViewControllers anyObject];
if (vc) {
[self.reusedViewControllers removeObject:vc]; }else{
PaperQuestionViewController *childVc = [[PaperQuestionViewController alloc] init];
[self addChildViewController:childVc];
vc = childVc;
}
CGRect bounds = self.scrollview.bounds;//
CGRect vcFrame = bounds;
vcFrame.origin.x = CGRectGetWidth(bounds) * index;
vc.rectView = vcFrame;
vc.index = index;
vc.view.frame = vcFrame; // 最后在这个地方,更新模板VC中的信息
///更新信息处理
}

最新文章

  1. 项目游戏开发日记 No.0x000001
  2. js单击自动选择文本
  3. 对字符串进行简单的字符数字统计 探索java中的List功能
  4. 存储过程使用CTE 和 case when
  5. T-SQL 使用WITH高效分页
  6. MySQL客户端工具推荐
  7. Codeforces Round #127 (Div. 2)
  8. linux作业六——进程的描述和进程的创建
  9. 飘逸的python - 编码杂症之在字符串前面加u
  10. 树状数组 &amp;&amp; 线段树
  11. 任务定义器——SocketProcessor
  12. Linux Shell 命令--grep
  13. 删除zabbix数据库日志
  14. smarty实例
  15. 测试工具之RobotFramework安装
  16. arm cortex-m0plus源码学习(三)GPIO
  17. poj3041(最小顶点覆盖)
  18. java.lang.IllegalStateException: The specified child already has a parent. You must call removeView
  19. win7右下角的网络连接图标不见了~终极必杀技
  20. idea 控制台乱码

热门文章

  1. 2018.09.25 bzoj3572: [Hnoi2014]世界树(虚树+树形dp)
  2. 2018.09.12 poj3621Sightseeing Cows(01分数规划+spfa判环)
  3. 第四章 代词(Les pronoms )
  4. linux配置ip 网关 和dns(转)
  5. VMware + LInux + Xshell 连接环境设置(心得体会)
  6. 笔记:记录两个新接触的东东- required + placeholder
  7. 20170908工作日记--UML画类图、HTTP协议、Volley源码走读
  8. java分层
  9. shell 脚本 计算 1加到100 的和
  10. Spring容器中bean的生命周期以及关注spring bean对象的后置处理器:BeanPostProcessor(一个接口)