使用loadNibNamed加载自定义UITableViewCell显示内存泄漏

时间:2011-11-04 13:45:00

标签: iphone ios memory-leaks uitableview instruments

我正在使用自定义UITableViewCell,当我在真实设备上使用Instruments运行应用程序时,它会给我一个内存泄漏,泄漏位置就是我调用函数时:

cel = (TipsCell *)[[[NSBundle mainBundle] loadNibNamed:@"TipsCell" owner:self options:nil] objectAtIndex:0];

这是我的代码:

static NSString *TipIdentifier = @"TCell";
        TipsCell *cel = (TipsCell *)[tableView dequeueReusableCellWithIdentifier:TipIdentifier];

if (cel == nil){

        NSLog(@"New Cell Made here");

        cel = (TipsCell *)[[[NSBundle mainBundle] loadNibNamed:@"TipsCell" owner:self options:nil] objectAtIndex:0];

        cel.txtTip.backgroundColor=[UIColor clearColor];
        cel.txtLikes.backgroundColor=[UIColor clearColor];
        cel.txtComments.backgroundColor=[UIColor clearColor];
        cel.txtUserName.backgroundColor=[UIColor clearColor];

        [cel.txtTip setLineBreakMode:UILineBreakModeWordWrap];
        [cel.txtTip setMinimumFontSize:FONT_SIZE];
        [cel.txtTip setNumberOfLines:0];


        cel.backgroundColor = [UIColor colorWithRed:0.113 green:0.42 blue:0.427 alpha:.9];

        //cell.backgroundColor = [UIColor colorWithRed:0.4 green:0.745 blue:0.835 alpha:.9];
        [cel setSelectionStyle:UITableViewCellSelectionStyleGray];
        cel.opaque = NO; 
    }

    /*
    if ([[QuestionsiLike objectAtIndex:indexPath.row] isEqual:@"1"]) {
        [cell.txtLikes setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    else{
        [cell.txtLikes setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }*/

    [cel.txtLikes setTitle:[QuestionsNoLikes objectAtIndex:indexPath.row] forState:UIControlStateNormal];

    [cel.txtLikes setTag:indexPath.row];

    [cel.txtLikes addTarget:self action:@selector(Like:) forControlEvents:UIControlEventTouchUpInside];

    cel.txtComments.text=[QuestionsNoComments objectAtIndex:indexPath.row];

    cel.txtTip.text=[QuestionsTxt objectAtIndex:indexPath.row];
    cel.txtUserName.text=[QuestionsUN objectAtIndex:indexPath.row];

    UIImage *image = [[TKImageCenter sharedImageCenter] imageAtURL:[NSString stringWithFormat:@"http://www.tipntag.com%@",[QuestionsUimg objectAtIndex:indexPath.row]] queueIfNeeded:YES];

    [cel.imgUser setImage:image];

    image = [[TKImageCenter sharedImageCenter] imageAtURL:[NSString stringWithFormat:@"http://www.tipntag.com%@",[QuestionsRankImg objectAtIndex:indexPath.row]] queueIfNeeded:YES];

    [cel.imgRank setImage:image];

    NSString *text =[QuestionsTxt objectAtIndex:indexPath.row];// [items objectAtIndex:[indexPath row]];

    CGSize constraint = CGSizeMake(220, 20000.0f);
    CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
    [cel.txtTip setFrame:CGRectMake(70, 5, 220 , MAX(size.height, 30.0f))];

    //cell.txtLikes.text=[QuestionsNoLikes objectAtIndex:indexPath.row];
    //cell.txtComments.text=[QuestionsNoComments objectAtIndex:indexPath.row];

    NSString *tstr=[NSString stringWithFormat:@"%@",[QuestionsLat objectAtIndex:indexPath.row]];
    NSLog(tstr);

    if ([tstr isEqual:@"<null>"]) {
        [cel.imgMap setEnabled:NO];
        [cel.lblDistance setHidden:YES];
        [cel.GetDirections setHidden:YES];
        [cel.imgDistance setHidden:YES];
    }
    else{
        [cel.imgMap setEnabled:YES];

        [cel.imgMap setTag:indexPath.row];

        [cel.imgMap addTarget:self action:@selector(GoToMap:) forControlEvents:UIControlEventTouchUpInside];

        NSLog(@"%@",[QuestionsDistance objectAtIndex:indexPath.row]);

        [cel.imgDistance setHidden:NO];
        [cel.lblDistance setHidden:NO];

        NSString *tmp2=[[NSString alloc]initWithFormat:[NSString stringWithFormat:@"%@",[QuestionsDistance objectAtIndex:indexPath.row]]];

        NSString *tmp=[[NSString alloc] initWithFormat:@"%@ mi",[tmp2 substringToIndex:4]];

        [tmp2 release];

        [cel.lblDistance setText:[NSString stringWithString:tmp]];

        [tmp release];

        [cel.GetDirections setHidden:NO];

        end.latitude=[[QuestionsLat objectAtIndex:indexPath.row] floatValue];
        end.longitude=[[QuestionsLong objectAtIndex:indexPath.row] floatValue];

        [cel.GetDirections setTag:indexPath.row];

        [cel.GetDirections addTarget:self action:@selector(getDirection:) forControlEvents:UIControlEventTouchUpInside];

    }

    if ([[QuestionsiLike objectAtIndex:indexPath.row] intValue]==0) {

        [[cel txtLikes] setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }else
    {
        [[cel txtLikes] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    NSLog(@"%@",[QuestionsLat objectAtIndex:indexPath.row]);

    return cel;

我尝试了很多,但它仍然给我这个泄漏,这导致我的应用程序崩溃,任何想法为什么我有这个泄漏

1 个答案:

答案 0 :(得分:0)

万一你不知道,Apple发布了ARC(自动引用计数),这有助于消除这些泄漏。您可以进入Refactor菜单并重构为AR​​C。

以下是有用的指南/帖子:http://longweekendmobile.com/2011/09/07/objc-automatic-reference-counting-in-xcode-explained/