Ich habe einen UIViewController, der modal präsentiert wird. Wenn ich das Speicherzuweisungen-Instrument beobachte, erhöht sich die Speichernutzung, wenn die Ansicht dargestellt wird, aber wenn sie beendet wird, wird der Speicher nicht freigegeben. Wenn ich die Ansicht öffne und schließe, wird der Speicher immer höher. Instrumente melden kein Speicherleck! Was könnte das verursachen? Der View-Controller-Code ist unten (ich habe den didSelectRow-Code übersprungen). Dealloc wird immer aufgerufen.Möglicher Speicherverlust in UIViewController mit UITableView
EDIT - Ich bin mit ARC
.h
#import <UIKit/UIKit.h>
@class OutlineTextUILabel;
@interface StoreViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
int starCount;
NSMutableArray *_singleUseArray;
NSMutableArray *_fullUseArray;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet OutlineTextUILabel *starCountLbl;
- (IBAction)exitBtnPressed:(id)sender;
.m
#import "StoreViewController.h"
#import "NSUserDefaults+MPSecureUserDefaults.h"
#import "PowerUpCell.h"
#import "OutlineTextUILabel.h"
#import "PowerUpSingleton.h"
#import "PowerUp.h"
#define kPrefsNumberOfStars @"numberOfStars"
@interface StoreViewController()
@end
@implementation StoreViewController
@synthesize tableView = _tableView;
@synthesize starCountLbl;
#pragma mark View Methods
- (void)viewDidLoad
{
[super viewDidLoad];
// Display star count
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
BOOL valid = NO;
starCount = [prefs secureIntegerForKey:kPrefsNumberOfStars valid:&valid];
if (!valid) {
NSLog(@"Stars Tampered With!");
self.starCountLbl.text = @"Err";
} else {
self.starCountLbl.text = [NSString stringWithFormat:@"%d",starCount];
}
// Tableview setup
CGRect frame2 = CGRectMake(0, 0, 320, 40);
UIView *footer = [[UIView alloc] initWithFrame:frame2];
footer.backgroundColor = [UIColor clearColor];
self.tableView.tableFooterView = footer;
self.tableView.opaque = NO;
self.tableView.backgroundView = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
if (![[PowerUpSingleton sharedList] refreshArray]) {
NSLog(@"Error, %s",__FUNCTION__);
} else {
[self performSelectorOnMainThread:@selector(workOutSingleUseToDisplay) withObject:nil waitUntilDone:YES];
[self performSelectorOnMainThread:@selector(workOutFullUseToDisplay) withObject:nil waitUntilDone:YES];
[self.tableView reloadData];
}
}
- (void)workOutSingleUseToDisplay
{
_singleUseArray = [[NSMutableArray alloc] init];
for (PowerUp *pu in [[PowerUpSingleton sharedList] sharedArray]) {
if (!pu.fullUnlock) {
[_singleUseArray addObject:pu];
}
}
}
- (void)workOutFullUseToDisplay
{
_fullUseArray = [[NSMutableArray alloc] init];
for (PowerUp *pu in [[PowerUpSingleton sharedList] sharedArray]) {
if (pu.prefFullName != nil) {
[_fullUseArray addObject:pu];
}
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
- (void)viewDidUnload {
[self setTableView:nil];
[self setStarCountLbl:nil];
[super viewDidUnload];
}
#pragma mark TableView Setup Methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (section == 0) {
return @"Single Use";
} else if (section == 1) {
return @"Use forever";
}
return nil;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0) {
return [_singleUseArray count];
} else if (section == 1) {
return [_fullUseArray count];
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier;
if (indexPath.section == 0) {
cellIdentifier = @"powerUpCellSingleUse";
} else if (indexPath.section == 1) {
cellIdentifier = @"powerUpCell";
}
PowerUpCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[PowerUpCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
if (indexPath.section == 0) {
PowerUp *tmpPU = [_singleUseArray objectAtIndex:indexPath.row];
cell.descriptionLbl.text = tmpPU.displayName;
int cost = tmpPU.costSingle;
cell.costLbl.text = [NSString stringWithFormat:@"%d",cost];
if (cost > starCount) {
cell.costLbl.textColor = [UIColor redColor];
} else {
cell.costLbl.textColor = [UIColor blueColor];
}
int howMany = tmpPU.numberOwned;
cell.howManyLbl.text = [NSString stringWithFormat:@"%d",howMany];
} else if (indexPath.section == 1) {
PowerUp *tmpPU = [_fullUseArray objectAtIndex:indexPath.row];
cell.descriptionLbl.text = tmpPU.displayName;
int cost = tmpPU.costFull;
cell.costLbl.text = [NSString stringWithFormat:@"%d",cost];
if (cost > starCount) {
cell.costLbl.textColor = [UIColor redColor];
} else {
cell.costLbl.textColor = [UIColor blueColor];
}
if (tmpPU.fullUnlock) {
cell.costLbl.textColor = [UIColor greenColor];
cell.costLbl.text = @"---";
}
}
return cell;
}
#pragma mark -
- (IBAction)exitBtnPressed:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
}
- (void)dealloc
{
NSLog(@"%s",__FUNCTION__);
self.tableView = nil;
self.starCountLbl = nil;
}
@end
EDIT ------------- Etwas nicht scheint Recht haben. Ich habe einen NSLog zur Zellenallokierung hinzugefügt und er wird nie aufgerufen, obwohl die Zellen erstellt wurden!
PowerUpCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSLog(@"new cell");
cell = [[PowerUpCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
EDIT 1. Juli ------ Ich habe eine Navigation Controller hinzugefügt und jetzt Push anstelle von modalen und dieses Problem ist noch da. Ich habe mit Aufnahmen Shapes gemacht, indem ich ein paar Mal zwischen den Ansichten hin- und herwechsle und es scheint, als ob die Zellen immer noch herumhängen, da dieser Screenshot zeigt, dass Gestenerkennung noch von einer vorherigen Ladung der Ansicht besteht.
Innen viewWillAppear verwenden Sie performOnMainThread. Dies wird nicht benötigt, da viewWillAppear im Hauptthread passiert. –
Ich habe nur diese Methode verwendet, also kann ich waitUntilDone: YES einstellen, damit ich jetzt die Arrays gefüllt habe, bevor ich die Tabelle gezeichnet habe. – Darren
Versuchen Sie Folgendes: [self workOutFullUseToDisplay]. Sie erkennen, Objective-C ist sequenziell richtig? –