2010-03-02 22 views
0

SO,iphone tableview单元内存泄漏?

我有一个tableview有20个单元格,每个单元格有一个文本标题,一个字幕标签和一个文本框(带一个按钮给它一个很好的背景),但我发现滚动tableview几次后它开始在模拟器中变慢。 我想这是一个内存泄漏,但我想我已经发布了我需要的一切。对我的(业余)代码有任何意见?

注意:以前我曾尝试在Quartz中使用圆角的textviews中绘制框,但是在滚动框时这也显着减慢了。请在破坏我的代码之前查看内存泄露; P

注2:PS对于自由使用代码,我无法找到泄漏。

问候

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 


    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 
     CGRect frame; frame.origin.x = 5; frame.origin.y = 10; frame.size.width = 20; frame.size.height = 25; 

     //Disallow Selection (Blue Flash) 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     //Print Icon 
     UIImageView *imgLabel = [[UIImageView alloc] initWithFrame:frame]; 
     imgLabel.tag = 1; 
     [cell.contentView addSubview:imgLabel]; 
     [imgLabel release]; 

     //Print Text 
     frame.origin.x = 30; 
     frame.origin.y = 5; 
     frame.size.width = CONST_Cell_width;  
     UILabel *nameLabel = [[UILabel alloc] initWithFrame:frame]; 
     nameLabel.tag = 100; 
     [cell.contentView addSubview:nameLabel]; 
     [nameLabel release]; 

     //subtitleLabel Text 
     UILabel *subtitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 32, CONST_Cell_width, 40)]; 
     subtitleLabel.tag = 101; 
     [cell.contentView addSubview:subtitleLabel]; 
     [subtitleLabel release]; 



//  detailLabel.layer.cornerRadius = 10; 


    } 
    //This bit is good! 
    //cell.textLabel.text   = [[contentArray objectAtIndex:indexPath.row] objectForKey:@"Title"]; 
    //cell.detailTextLabel.text = [[contentArray objectAtIndex:indexPath.row] objectForKey:@"Description"]; 
    //Sets wrapping correctly 
    //cell.textLabel.numberOfLines = 0; 
    //cell.detailTextLabel.numberOfLines = 0; 



    //Setup Name to Cell 
    UILabel * nameLabel = (UILabel *) [cell.contentView viewWithTag:100]; 
    [nameLabel setFont:[UIFont boldSystemFontOfSize:20.0]]; 
    nameLabel.text = [[contentArray objectAtIndex:indexPath.row] objectForKey:@"Title"]; 
    nameLabel.textColor = [UIColor blackColor]; 
    nameLabel.backgroundColor = [UIColor clearColor]; 
    nameLabel.numberOfLines = 1; 

    //Setup Subtitle 
    UILabel * subtitleLabel = (UILabel *) [cell.contentView viewWithTag:101]; 
    [subtitleLabel setFont:[UIFont systemFontOfSize:15.0]]; 
    subtitleLabel.textColor = [UIColor grayColor]; 
    subtitleLabel.text = [[contentArray objectAtIndex:indexPath.row] objectForKey:@"Description"]; 
    subtitleLabel.backgroundColor = [UIColor clearColor]; 
    subtitleLabel.numberOfLines = 2; 


     //A Nice Button for rounded background effect..cheap i know 
     UIButton *roundedButtonType = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain]; 
     roundedButtonType.frame = CGRectMake(30, 80, CONST_Cell_width, CONST_Text_height); 
     roundedButtonType.userInteractionEnabled = NO; 
     roundedButtonType.backgroundColor = [UIColor clearColor]; 
     [cell.contentView addSubview:roundedButtonType]; 
     [roundedButtonType release]; 

     UITextView *detailLabel = [[UITextView alloc] initWithFrame:CGRectMake(30, 80, CONST_Cell_width, CONST_Text_height)]; 
     detailLabel.tag = indexPath.row; 
     detailLabel.backgroundColor = [UIColor clearColor]; 
     detailLabel.text = [NSString stringWithFormat:@"%d",indexPath.row]; 
     detailLabel.font = [UIFont fontWithName:@"Helvetica" size:17]; 
     detailLabel.delegate = self; 
     [cell.contentView addSubview:detailLabel]; 
     [detailLabel release]; 

     //NSLog(@"Made my way to cellForRowAtIndexPath. Enter some data!!!\n"); 
     //DataPoint Name 
     NSString *keyname = [NSString stringWithFormat:@"data%d",indexPath.row +1]; 
     NSString *datavalue = [clientDataArray objectForKey:keyname]; 

     //DataValue (can be null) 
     //NSString *dataValue = [clientDataArray objectForKey:keyname] ; 
    if (datavalue == [NSNull null]) datavalue = @"(blank)"; 



    detailLabel.text = datavalue; 



    return cell; 
    [datavalue release]; 
    [keyname release]; 
    [clientDataArray release]; 


} 

回答

1

杀了你的应用程序单元格,每次用户触摸tableView。尝试把一个NSLog(@“我跑!”);之前return cell;

(回归后的代码不会被执行,即所有的内存释放未完成)

好吧,你所给予的意见标签做正确的事,所以你可以稍后参考它们。 做所有的UILabel的东西,如果(细胞==零)环内以及再循环外,return cell使用标签来设置标签值之前:

((UILabel *)[cell viewWithTag:14]).text = [myDataArray objectAtIndex:indexPath.row]; 

筛选。

if(cell == nil)块中的代码仅在单元格之前没有显示过,即已经在整个时间之外显示过的情况下运行。其余代码始终运行使用tableView。

希望它有道理:)

0

尝试与性能工具“泄漏”

在Xcode中,单击运行>与性能工具运行“泄漏

任何泄漏显示为红色线条,在运行可以选择检测到泄漏的时间线上的部分,并通过检查堆栈来查找泄漏。如果这一切听起来有点复杂,那么只需简单地遍历代码,并确保您发布了使用alloc,new,copy或其变体创建的任何内容,则应该捕获大多数潜在的泄漏。

顺便说一句,一个简单的泄漏不会迅速,除非它已经进入一个巨大的循环或外部的,如果(细胞==零)循环运行每一个可见的所有代码的东西