static NSString *CellTableIdentifier = @"CellTableIdentifier ";
 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
 
CellTableIdentifier];
 
if (cell==nil) {
 
cell = [[[UITableViewCell alloc]
 
initWithStyle:UITableViewCellStyleDefault
 
reuseIdentifier:CellTableIdentifier] autorelease];
 
}

首先,先说一下重用是如何实现的,我们有一个很长的tableview,假设是100行,但在我们设计的行高情况下,屏幕只能显示10行,所以,当加载view的时候,实际只创建这10个cell,当我们用手指向上滑动即下方的第11个cell向上滑进界面的过程中,我们发现第一行的cell会滑出界面,当滑出后,第一行这个cell会被放到重用队列中,第11行将获得重用队列中的一个cell,这样,可以很好的控制内存。

.UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellTableIdentifier];

   意思是定义一个cell,在tableview中的可重用队列中寻找有CellTableIdentifier标识的UITableViewCell,以进行重用。这个Identifier可以由我们自由定义成一些字符串。当然,如果在队列中有这样的UITableViewCell,那么则返回它给cell,若没有,则返回nil给cell。因为我们在写程序时经常需要用到好几个表格,每个表格中的cell布局都不一样,所以我们需要将在同一个表格的cell(布局一样)进行标记Identifier,这样,在重用的时候可以得到匹配的cell。

但对于多变的自定义cell,有时这种重用机制会出错。比如,当一个cell含有一个UITextField的子类并被放在重用queue中以待重用,这时如果一个未包含任何子视图的cell要显示在屏幕上,就会取出并使用这个重用的cell显示在无任何子视图的cell中,这时候就会出错。

解决方法:

方法1 将获得cell的方法从- (UITableViewCell*)dequeueReusableCellWithIdentifier:(NSString*)identifier 换为-(UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

重用机制调用的就是dequeueReusableCellWithIdentifier这个方法,方法的意思就是“出列可重用的cell”,因而只要将它换为cellForRowAtIndexPath(只从要更新的cell的那一行取出cell),就可以不使用重用机制,因而问题就可以得到解决,虽然可能会浪费一些空间。

示例代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
     static NSString *CellIdentifier = @"Cell";
     // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //改为以下的方法
     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //根据indexPath准确地取出一行,而不是从cell重用队列中取出
     if (cell == nil) {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
     }
      //...其他代码                              
 }

方法2 通过为每个cell指定不同的重用标识符(reuseIdentifier)来解决。
重用机制是根据相同的标识符来重用cell的,标识符不同的cell不能彼此重用。于是我们将每个cell的标识符都设置为不同,就可以避免不同cell重用的问题了。

示例代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
     
     NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];//以indexPath来唯一确定cell
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell
     if (cell == nil) {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
     }
     //...其他代码
 }

       方法3 删除重用cell的所有子视图

这个方法是通过删除重用的cell的所有子视图,从而得到一个没有特殊格式的cell,供其他cell重用。

示例代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
     static NSString *CellIdentifier = @"Cell";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell
     if (cell == nil) {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
     }
     else
     {
         //删除cell的所有子视图
         while ([cell.contentView.subviews lastObject] != nil)
         {
             [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
         }
     }
     //...其他代码
 }