2011-06-04 82 views
0

我正在準備像這樣的定製單元格 1)創建了一個定製單元格與Xib 2)因爲我必須更改自定義單元格中的對應於值讀取從數據庫。我不能重複使用相同的cellIdentifieriphone:在內存消耗和內存泄漏的情況下表格視圖

static NSString *MyIdentifier = @"MyIdentifier"; 
MyIdentifier = [NSString stringWithFormat:@"Cell %d",indexPath.row]; 

NSString *offendersImagePath = [self applicationDocumentsDirectory]; 

offendersImagePath=[offendersImagePath stringByAppendingPathComponent:@"Images"]; 


CustomOffendersCell *cell = (CustomOffendersCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier]; 
if(cell == nil) { 
    [[NSBundle mainBundle] loadNibNamed:@"CustomOffendersCell" owner:self options:nil]; 
    cell = aCustomOffendersCell; 
    aCustomOffendersCell=nil; 
} 
    NSMutableArray *tempArray;//=[[NSMutableDictionary alloc] init]; 

tempArray=[offendersNamesList objectAtIndex:indexPath.row]; 

的代碼都像上面時,我檢查了儀器的表現泄露這件事對於這條線和高內存消耗

[[NSBundle mainBundle] loadNibNamed:@"CustomOffendersCell" owner:self options:nil]; 

你可以建議我一些方法來擺脫這個

回答

2

你應該叫 「釋放」 的方法對細胞像 「[aCustomOffendersCell發行]」。 此外,你必須釋放所有你已經在單元格中分配的數據,如UILabel,UIImageView等......所以,當你把它放入單元格內,然後釋放這些所有數據。

希望它對你有幫助。並讓我知道更多的細節。

1

你應該打電話autoreleaseaCustomOffendersCell,而不是隻分配nil

aCustomOffendersCell nil; 

使用下面

[aCustomOffendersCell autorelease]; 
+0

但'cell'指向同一個對象。可能autorelease? – 2011-06-04 07:26:19

+0

@Deepak:對,但(** cell **實例)將由'tableView'保留,那麼** aCustomOffendersCell **呢? – Jhaliya 2011-06-04 07:28:07

+1

是的,但它必須保持活力,直到方法結束。 'release'將主要銷燬'cell'指向的對象。但這是泄漏。 – 2011-06-04 07:31:36