iOS 关于UICollectionView的cell重用问题
来源:互联网 发布:淘宝二级页面打不开 编辑:程序博客网 时间:2024/06/02 08:35
UICollectionView重用机制减轻了内存的使用压力,但是有些时候我们并不希望cell被重用,
比如自定义的UICollectionViewCell里面包含了一个UIProgressView(进度条)。假如某一个cell里进度条的当前进度为50%,
你向上滑动屏幕,这个cell消失;当你再向下滑动时,发现刚刚那个cell是一个空的cell,进度条是0%.
怎么解决这个问题呢?
在UITableView里面里面可以用以下方法解决:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 定义唯一标识 static NSString *CellIdentifier = @"Cell"; // 通过indexPath创建cell实例 每一个cell都是单独的 UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; // 判断为空进行初始化 --(当拉动页面显示超过主页面内容的时候就会重用之前的cell,而不会再次初始化) 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;}重点在
[tableView cellForRowAtIndexPath:indexPath]会返回一个已经存在的cell,
然而UICollectionView的cellForItemAtIndexPath方法却不一样,它只会返回可见区域的cell.
<pre name="code" class="html"><img src="http://img.blog.csdn.net/20150905115634385?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
然后我们会想能不能不调用<span style="color: rgb(46, 13, 110); font-family: Menlo, monospace; line-height: 20.299999237060547px;">dequeueReusableCellWithReuseIdentifier:<span style="color: rgb(46, 13, 110); font-family: Menlo, monospace; line-height: 20.299999237060547px;">forIndexPath:方法而直接生成一个cell呢?</span></span>
<span style="color: rgb(46, 13, 110); font-family: Menlo, monospace; line-height: 20.299999237060547px;"><span style="color: rgb(46, 13, 110); font-family: Menlo, monospace; line-height: 20.299999237060547px;">很不幸,UICollectionViewCell没有类似UITableViewCell的initWithStyle:reuseIdentifier:那样的方法,只能通过调用</span></span>
<span style="color: rgb(46, 13, 110); font-family: Menlo, monospace; line-height: 20.299999237060547px;"><span style="color: rgb(46, 13, 110); font-family: Menlo, monospace; line-height: 20.299999237060547px;">init方法。</span></span>
<span style="color: rgb(46, 13, 110); font-family: Menlo, monospace; line-height: 20.299999237060547px;"><span style="color: rgb(46, 13, 110); font-family: Menlo, monospace; line-height: 20.299999237060547px;">最后我的解决方法是通过一个NSDictionary保存已经生成cell,使用这种方法的前提是cell的数目不能太多。</span></span>
<span style="color: rgb(46, 13, 110); font-family: Menlo, monospace; line-height: 20.299999237060547px;"><span style="color: rgb(46, 13, 110); font-family: Menlo, monospace; line-height: 20.299999237060547px;">代码如下:</span></span>
<span style="color: rgb(46, 13, 110); font-family: Menlo, monospace; line-height: 20.299999237060547px;"><span style="color: rgb(46, 13, 110); font-family: Menlo, monospace; line-height: 20.299999237060547px;"></span></span><pre name="code" class="html">- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {UICollectionViewCell *cell = nil;NSString *reuseIdentifier = [NSString stringWithFormat:@"Cell%ld%ld", indexPath.section, indexPath.item];//_reuseDic是一个ivar, 通过_reuseDic = [NSMutableDictionary dictionary];生成if (_reuseDic[reuseIdentifier]) {cell = _reuseDic[reuseIdentifier];}else {cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];_reuseDic[reuseIdentifier] = cell;}return cell;}
0 0
- iOS 关于UICollectionView的cell重用问题
- UITableView和UICollectionView的cell重用问题
- iOS tableView的cell的重用问题
- 关于cell的重用标识符的问题
- 关于Nib加载cell重用的问题
- 几个关于TableView的Cell重用问题
- 关于cell的重用
- ios之清除cell缓存,解决cell的重用问题。
- cell的重用问题
- ios 中cell重用问题
- UICollectionView重用机制的问题
- ios UICollectionView的cell重叠
- iOS开发之关于cell的重用机制
- iOS关于TableView重用Cell的一点领悟
- UICollectionView的cell间距问题
- iOS开发-简述UITableView中cell的重用问题
- iOS 解决cell因重用机制出错的问题
- cell的注册重用问题
- 冗余 自动化 安全(优秀)
- 2小时玩转python基础(上)
- 漫谈C指针第四讲---指针与数组
- Navicat访问MySQL出现1044/1045错误的解决方法
- Java collections小知识收集
- iOS 关于UICollectionView的cell重用问题
- iOS资料大全收集
- 活该你单身......都好好单着吧
- HDU 1695 GCD(欧拉函数+容斥原理)
- php学习--3 流程控制语句,字符串操作
- phpstorm 8.x注册码
- HDU5011-Game-西安赛区网络赛-博弈论-水题
- 2.1.3 Debugging of Shaders (about vertex input parameters) 着色器的调试(关于顶点输入参数)
- 数据结构基础题(第一篇)