0

所以,

我有一个包含 20 个单元格的表格视图,每个单元格都有一个文本标题、一个字幕标签和一个文本字段(带有一个按钮可以给它一个漂亮的背景),但我发现在滚动表格视图几次后它开始在模拟器中变慢. 我认为这是内存泄漏,但我认为我已经发布了我需要的所有内容。对我的(业余)代码有任何评论吗?

注意:我之前曾尝试在 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];


}
4

2 回答 2

1

每次用户触摸 tableView 时,if(cell == nil) 循环之外的所有代码都会针对每个可见单元格运行。尝试放置一个 NSLog(@"I ran!"); 就在之前return cell;

(返回后的代码不会运行,即你所有的内存释放都没有完成)

好的,通过给定视图标签,您正在做正确的事情,以便您以后可以参考它们。return cell在使用标签设置标签值之前,在 if(cell == nil) 循环内执行所有 UILabel 内容,然后在循环外执行所有操作:

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

像这样。

if(cell==nil) 块中的代码仅在单元格之前未显示时运行,即一直不在屏幕上等。其余代码在使用 tableView 时一直运行。

希望这是有道理的:)

于 2010-03-02T23:17:25.957 回答
0

尝试使用性能工具“Leaks”运行

在 Xcode 中,单击运行 > 使用性能工具运行 > 泄漏

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

顺便说一句,一个简单的泄漏不会很快杀死你的应用程序,除非它进入一个巨大的循环或其他东西

于 2010-03-02T23:06:56.920 回答