2011-11-12 70 views
0

我正在使用自定義UITableViewCell,當我在真實設備上使用儀器運行應用程序時,它給我一個內存泄漏。加載導致內存泄漏的自定義uitableviewcell

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; 
} 

我嘗試了很多與它,但我的代碼仍然漏水:當我調用該方法cellForRowAtIndexPath發生了泄漏。我相信這個漏洞會導致崩潰。

回答

0

試試這個嗎?

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

如果你確實需要加載的筆尖文件,在initWithStyle運行:
我不確定這是否是導致內存泄漏的真正問題。 :?