加载导致内存泄漏的自定义uitableviewcell

时间:2011-11-12 00:54:19

标签: iphone uitableview memory-leaks uitabbarcontroller

我正在使用自定义UITableViewCell,当我在真实设备上使用Instruments运行应用程序时,它会给我一个内存泄漏。当我调用方法cellForRowAtIndexPath

时发生泄漏
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)

试试这个?

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

如果您确实需要加载nib文件,请在 initWithStyle:中运行。
我不确定这是否是导致内存泄漏的真正问题。 :