iOS 关于UICollectionView的cell重用问题



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="https://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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值