首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在哪里/如果释放UITableViewCell中的属性

在哪里/如果释放UITableViewCell中的属性
EN

Stack Overflow用户
提问于 2011-08-29 03:02:58
回答 2查看 687关注 0票数 0

编辑:好的,我想出了如何解决我原来的问题,但我不确定这是不是最好的方法。

我的新问题是,假设我有一个UITableViewCell的子类,在标题中有以下属性声明:

代码语言:javascript
复制
@property (nonatomic, retain) IBOutlet UILabel *levelLabel;

这是在IB中连接的。不在dealloc中发布它,并且根本不发布它,这是可以的吗?这是我唯一能想出让它工作的方法,而不会给我一个exc_bad_access错误。以前,当tableviewcell单元格从屏幕上消失时,它会调用dealloc,但之后它仍然需要它。我在哪里发布东西,或者它会帮我解决这个问题?

原标题: UITableView和exc_bad_access中的内存泄漏Ok,我很困惑我跟随这篇在线教程制作定制的UITableViewCells。我做了一个,我按照教程告诉我的做了所有的事情。我的UITableViewCell子类包含3个UILabels和3个UIButtons,并将它们都定义为属性并在IB中连接。我需要它们对班级可用,因为我需要知道按钮何时被按下,并能够更改文本。当我运行这个应用程序时,我开始滚动,几秒钟后它崩溃了,exc_bad_access在main中(控制台中没有输出)。但当我用NSZombieEnabled在仪器中运行这个应用程序时,它根本不会崩溃,而且运行得很好。但是,由于instruments会向您显示分配,我可以看到它们很快就会上升,特别是当我滚动时。我不知道这是不是所有的分配,或者如果这些是释放,但仍然看起来太快了。

这是PointCoordinatesCell.h (我的自定义单元格):

代码语言:javascript
复制
#import <UIKit/UIKit.h>

@interface PointCoordinatesCell : UITableViewCell


@property (nonatomic, retain) IBOutlet UILabel *levelLabelLabel;
@property (nonatomic, retain) IBOutlet UILabel *levelLabel;
@property (nonatomic, retain) IBOutlet UILabel *levelDescriptionLabel;
@property (nonatomic, retain) IBOutlet UIButton *beginningButton;
@property (nonatomic, retain) IBOutlet UIButton *developingButton;
@property (nonatomic, retain) IBOutlet UIButton *secureButton;

@end

PointCoordinatesCell.m:

代码语言:javascript
复制
#import "PointCoordinatesCell.h"

@implementation PointCoordinatesCell
@synthesize levelLabel, levelLabelLabel, levelDescriptionLabel, beginningButton, developingButton, secureButton;

- (void)dealloc{
    [super dealloc];
    [levelLabel release];
    [levelLabelLabel release];
    [levelDescriptionLabel release];
    [beginningButton release];
    [developingButton release];
    [secureButton release];
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

RootViewController.h中只有一个类声明和标准导入。未定义任何变量或方法。它是UITableViewController的子类。

RootViewController.m:

代码语言:javascript
复制
#import "RootViewController.h"
#import "StatesAppDelegate.h"
#import "PointCoordinatesCell.h"

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}

#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 50;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    return 293;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"PointCoordinatesCell";

    PointCoordinatesCell *cell = (PointCoordinatesCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {

        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"PointCoordinatesCell" owner:self options:nil];

        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (PointCoordinatesCell *) currentObject;
                break;
            }
        }
    }

    //cell.capitalLabel.text = [capitals objectAtIndex:indexPath.row];
    //cell.stateLabel.text = [states objectAtIndex:indexPath.row];

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    // AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
    // [self.navigationController pushViewController:anotherViewController];
    // [anotherViewController release];
}



- (void)dealloc {
    [super dealloc];
}


@end
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-08-29 03:23:05

看起来你在你的cellForRowAtIndexPath方法中做了一些复杂的转换。我认为这是没有必要的。对我来说,检查UITableViewCell类的对象,然后将其强制转换为自定义单元格似乎不符合逻辑。nib中的单元格应该已经是一个自定义单元格。

在Apple示例中,单元的加载要简单得多。将自定义单元格链接到视图控制器中的IBOutlet,然后执行以下操作:

代码语言:javascript
复制
CustomCell *cell = (CustomCell *) [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    cell = customTableViewCell;
    self.customTableViewCell = nil;
    // etc.
}
票数 0
EN

Stack Overflow用户

发布于 2011-08-29 03:21:31

不,不发布标签是不好的。您使用‘retain’说明符声明了该属性。这意味着你必须至少在dealloc中释放它(一种安全的方式是:self. levelLabel = nil;)。

正如你已经注意到的,如果你不释放对象,在滚动过程中内存消耗将会增加(内存泄漏!)。

您必须告诉我们exc_bad_access错误发生的位置,以便我们能够帮助您...

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7223189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档