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