---
title: "cell重用问题"
author: "xiaotongeye"
date: "2014-11-01T20:44:14Z"
tags: []
categories: ["编程语言"]
slug: "cell"
draft: false
lastmod: "2014-11-01T20:44:14Z"
id: 2151018
url: "https://xiaotongeye.iteye.com/blog/2151018"
---

解决问题之前，在cell上添加了label，但是给label添加label.text之后滑动时即用到cell重用时label的内容总是重叠，从网上搜了好多，现在终于解决了，总结一下我用的方法：就是给重用的cell也设置不同的标示符。首先，对于tableviewcell

```这是我们很熟悉的一段cell重用的代码：

    static NSString *CellIdentifier =@"Cell";
    UITableViewCell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier];
    if (cell == nil)
    {
        cell = [[uitableviewcell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

```

只需要在定义标示符的时候换成这句就可以了
``` 

NSString *CellIdentifier = [nsstring stringwithformat:@"cell%d",indexpath.row];

```

这样就能保证每一行的标示符都不一样，也就不会出现重用问题。
还有在collectionViewcell时用同样的方法解决

``` 

NSString *CellIdentifier = [nsstring stringwithformat:@"cell%d",indexpath.row];
    [m_collectionview registerclass:[uicollectionviewcell class] forCellWithReuseIdentifier:CellIdentifier];
        UICollectionViewCell *cell = [collectionview dequeuereusablecellwithreuseidentifier:cellidentifier forindexpath:indexpath];

```