加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – 使用UITableView的UIViewController中可能的内存

发布时间:2020-12-14 17:53:40 所属栏目:百科 来源:网络整理
导读:我有一个UIViewController,它以模态方式呈现.当我观察内存分配Instrument时,显示视图时内存使用量会增加,但是当它退出时,内存不会被释放. 如果我继续打开和关闭视图,内存会不断增加. 仪器不会报告内存泄漏! 可能是什么导致了这个? View Controller代码如下
我有一个UIViewController,它以模态方式呈现.当我观察内存分配Instrument时,显示视图时内存使用量会增加,但是当它退出时,内存不会被释放.
如果我继续打开和关闭视图,内存会不断增加.
仪器不会报告内存泄漏!
可能是什么导致了这个? View Controller代码如下(我跳过了didSelectRow代码).
总是叫Dealloc.

编辑 – 我正在使用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,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

编辑————-
似乎有点不对劲.我已经向单元格alloc添加了一个NSLog,即使创建了单元格,它也从未被调用过!

PowerUpCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        NSLog(@"new cell");
        cell = [[PowerUpCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

编辑7月1日——
我添加了一个导航控制器,现在使用push而不是modal,这个问题仍然存在.
我通过在视图之间前后移动几次来使用乐器拍摄镜头,看起来可能是单元格仍然悬空,因为此屏幕截图显示手势识别器仍然在前一次加载视图时.

解决方法

这是因为你使用IBOutlets弱,而不是使用强.

我实际上认为这是XCode环境中的一个缺陷,因为它应该警告你这种行为.

作为一种最佳实践,我建议让XCode通过将视图拖动到Interface Builder中的代码来生成IBOutlets,以避免这种烦人的陷阱.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读