2009-12-21 40 views
0

我正在使用Instruments來監視內存使用情況。我注意到內存總是增加,而不是在我的UITableViewController上釋放。我不明白爲什麼。下面我列出我的代碼如何使用UITableViewController釋放內存

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    int row = [indexPath section]; 
    static NSString *CellIdentifier = @"ApplicationCell"; 
    BookCell *cell = (BookCell *)[tableViewdequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil) { 
     [[NSBundle mainBundle] loadNibNamed:@"BookCell" owner:self options:nil]; 
     cell = bookCell; 
     self.bookCell=nil; 
    } 
    Book *book = [books objectAtIndex:row]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    cell.bookNameLabel.text = book.author; 
    cell.bookPriceLabel.text = book.price; 
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; 
     return cell; 
} 
+0

什麼是儀器告訴你沒有發佈?通常它會告訴你什麼類沒有被釋放,以及在調用堆棧中的什麼地方。 – 2009-12-21 09:07:22

+0

我解決了這個問題。我忘了在我自己定製的CellView中釋放一些元素 – 2009-12-22 01:06:15

回答

0

枝......

試試看這種方式,我希望我會工作

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {int row = [indexPath section];

    if(cell == nil){[NSBundle mainBundle] loadNibNamed:@「BookCell」owner:self options:nil]; cell = bookCell; self.bookCell = nil; }
    Book * book = [books objectAtIndex:row]; cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.bookNameLabel.text = book.author;
    cell.bookPriceLabel.text = book.price; 單元格。accessoryType = UITableViewCellAccessoryDisclosureIndicator; return cell; }

0

答案#1:

如果你指的是內存分配和不泄漏內存,這裏是一個建議。

實施viewDidUnload釋放不再需要的視圖控制器的部分。通常,如果您不使用xib文件,您將在viewDidLoad(或loadView)中重新實現這些對象。

你可以通過把的NSLog語句中viewDidUnload和iPhone模擬器想象這一點,並單擊「硬件 - >模擬內存警告」,除非他們已經卸載,大部分的非可見viewcontrollers將運行viewdidunload方法。

答案#2:

如果你指的是內存泄漏,那麼我會懷疑你是加載表格單元格的方法是泄漏內存。 Here is a good reference for loading tablecells from xibs。你的方法可能是正確的,我只是不熟悉它。

0

你的代碼看起來對我來說是無泄漏的。

我也注意到下面的行看起來應該會導致您的自定義單元格對象年初發布(假設bookCell屬性被聲明爲「保留」):

self.bookCell=nil; 

我建議完全刪除。你也可以將其更改爲(假定實例變量名稱相匹配的屬性名):

bookCell=nil;