iOS 解决cell因重用机制出错的问题

  • Post author:
  • Post category:其他


1 不用重用

- (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];  
    }  
    //...其他代码  
} 

2 删除重用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];  
        }  
    }  
    //...其他代码  
}

转载的链接

http://blog.csdn.net/iqrocket/article/details/8812882