开发者

How to release memory as using UITableViewController

开发者 https://www.devze.com 2022-12-14 08:02 出处:网络
I\'m using Instruments to monitor memory usage. I notice that memory always increase and not release anymore on my UITableViewController. I don\'t understand why. Following I list my codes

I'm using Instruments to monitor memory usage. I notice that memory always increase and not release anymore on my UITableViewController. I don't understand why. Following I list my codes

- (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 = UITableViewCellSelectionStyleNon开发者_如何转开发e;
    cell.bookNameLabel.text = book.author;  
    cell.bookPriceLabel.text = book.price;
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        return cell;
}


Hie...

Just try it this way, i hope i would work

  • (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; cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; return cell; }


Answer #1:

If you are referring to memory allocation and not leaking memory, here is a suggestion.

Implement viewDidUnload to release parts of your viewcontroller that are no longer needed. Typically, you would re-instantate these objects in viewDidLoad (or loadView if you are not using xib files).

You can visualize this by placing NSLog statements inside viewDidUnload and in iphone simulator and click "Hardware->Simulate Memory Warning", unless they already unloaded, most of your non-visible viewcontrollers will run the viewdidunload method.

Answer #2:

If you are referring to memory leaks, then I would suspect the way you are loading table cells is leaking memory. Here is a good reference for loading tablecells from xibs. Your method might be correct, I'm just not familiar with it.


Your code looks leak-free to me.

I also noticed that the following line looks like it should be causing release of your custom cell object early (assuming the bookCell property is declared as "retain"):

self.bookCell=nil;

I would recommend removing it completely. You could also change it to (assuming the instance variable name matches the property name):

bookCell=nil;
0

精彩评论

暂无评论...
验证码 换一张
取 消