2014-09-12 73 views
0

我每次向上和向下滾動我的UITableView時都會增加內存使用量。我使用dequeueReusableCellWithIdentifier,但它似乎並沒有優化內存使用。這裏是代碼:UITableView dequeueReusableCellWithIdentifier滾動時可能泄漏

我認爲這是因爲每次分配UIImageView,但是當我註釋這些代碼行並且只留下標準的UITableViewCell實現時,內存問題不會消失。雖然離開視圖內存釋放後(obv它只發生在[self.tableView removeFromSuperview]方法)。但是,當我停留在視圖中並保持上下滾動時,內存就會增加。

@interface ArtistsViewController() 

@property (weak, nonatomic) IBOutlet UITableView *tableView; 

@end 

@implementation ArtistsViewController 

@synthesize fetchedResultsController = _fetchedResultsController; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSError *error; 
    if (![[self fetchedResultsController] performFetch:&error]) { 
     // Update to handle the error appropriately. 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     exit(-1); // Fail 
    } 
    self.tableView.delegate = self; 
    self.tableView.dataSource = self; 

    self.tableView.backgroundColor = [UIColor colorWithRed:11/255.0 green:12/255.0 blue:20/255.0 alpha:1.0]; 
    self.tableView.opaque = NO; 
    self.tableView.backgroundView = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

- (void)viewDidDisappear:(BOOL)animated { 
    [super viewDidDisappear:YES]; 
    [self.tableView removeFromSuperview]; 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    id sectionInfo = 
    [[_fetchedResultsController sections] objectAtIndex:section]; 
    return [sectionInfo numberOfObjects]; 
} 

- (void)loadCellData:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath { 
    Artist *artist = [_fetchedResultsController objectAtIndexPath:indexPath]; 
    NSLog(@"%@", artist.name); 
    cell.textLabel.text = [NSString stringWithFormat:@"%@", artist.name]; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%lu songs", (unsigned long)[artist.songs count]]; 

    NSString *fileName = [NSString stringWithFormat: @"%@/%@.png", [[NSBundle mainBundle] resourcePath], artist.name]; 

    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(20, 2, 55, 55)]; 

    imgView.image=[UIImage imageWithContentsOfFile:fileName]; 
    [cell.contentView addSubview:imgView]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (!cell) { 
     NSLog(@"NOCELL"); 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    } 

    //Load cell data 
    [self loadCellData:cell atIndexPath:indexPath]; 

    //customization 
    cell.contentView.backgroundColor = cell.selectedBackgroundView.backgroundColor = [UIColor colorWithRed:11/255.0 green:12/255.0 blue:20/255.0 alpha:1.0]; 
    cell.textLabel.textColor = [UIColor colorWithWhite:222/255.0 alpha:1.0]; 
    cell.detailTextLabel.textColor = [UIColor colorWithRed:62/255.0 green:103/255.0 blue:115/255.0 alpha:1.0]; 

    return cell; 
} 

#pragma mark - fetchedResultsController 

- (NSFetchedResultsController *)fetchedResultsController { 

    if (_fetchedResultsController != nil) { 
     return _fetchedResultsController; 
    } 

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription 
            entityForName:@"Artist" inManagedObjectContext:self.managedObjectContext]; 
    [fetchRequest setEntity:entity]; 

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] 
           initWithKey:@"name" ascending:YES]; 
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]]; 


    NSFetchedResultsController *theFetchedResultsController = 
    [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
             managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil 
                cacheName:@"Root"]; 
    self.fetchedResultsController = theFetchedResultsController; 
    _fetchedResultsController.delegate = self; 

    return _fetchedResultsController; 

} 

回答

0

添加自定義單元格而不是使用默認的單元格屬性。

static NSString *MyIdentifier = @"MyIdentifier"; 
     MyCustomCell *cell = (MyCustomCell *)[tableView dequeueReusableCellWithIdentifier: MyIdentifier]; 
     if (cell == nil) { 
      NSArray *nib; 

       nib = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" 
                owner:self options:nil]; 


      for (id oneObject in nib) if ([oneObject isKindOfClass:[MyCustomCell class]]) 
       cell = (YorBillTableCell *)oneObject; 
+0

沒有幫助。仍在泄漏。似乎是自2009年以來的泄漏。 – autobot 2014-09-12 20:58:28