2011-08-28 79 views
0

编辑:好的,所以我想出了如何弥补我原来的问题,但我不知道这是否是最好的方法。在哪里/如果释放UITableViewCell属性

我的新问题是,说我的UITableViewCell的头中具有以下属性声明一个子类:

@property (nonatomic, retain) IBOutlet UILabel *levelLabel; 

这是连接IB。在dealloc中不释放它,可以不释放它吗?这是我可以找到它的唯一方法,而不会给我一个exc_bad_access错误。之前,当tableviewcell离开屏幕但是仍然需要它时,它调用dealloc。我在哪里发布的东西,还是它为我照顾?

原标题:UITableView和exc_bad_access中的内存泄漏好吧,我很困惑。我在线制作自定义的UITableViewCells。我做了一个,我做了一切像教程告诉我的。我的UITableViewCell子类包含3个UILabel和3个UIButton,并且它们都被定义为属性并在IB中连接。我需要他们对课程可用,因为我需要知道何时按下按钮并能够更改文本。当我运行应用程序时,我开始滚动,并在几秒钟后崩溃,在main中使用exc_bad_access(控制台中没有输出)。但是当我用NSZombieEnabled在乐器中运行应用程序时,它根本不会崩溃,并且运行得很好。但是,由于工具向您显示了分配,所以我可以很快看到它们的增加,尤其是当我滚动时。我不知道这是否是所有的分配,或者如果这些都被释放,但仍然看起来太快。

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

#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:

#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:

#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 

回答

0

好像你正在做一些复杂的铸件在cellForRowAtIndexPath方法。我不认为这是必要的。对我来说,检查类UITableViewCell的对象然后将其转换为自定义单元格似乎不合逻辑。你的笔尖中的单元格应该已经是一个自定义单元格。

在Apple示例中,单元格的加载更直截了当。您在您的视图控制器连接您的自定义单元格到IBOutlet,然后做到这一点:

CustomCell *cell = (CustomCell *) [aTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil]; 
    cell = customTableViewCell; 
    self.customTableViewCell = nil; 
    // etc. 
} 
0

不,这是不行的,以不释放标签。你用'保留'说明符声明属性。这意味着你必须至少在dealloc中释放它(一个安全的方法是:self. levelLabel = nil;)。

正如您已经注意到,如果您不释放对象(内存泄漏!),滚动期间内存消耗将会增加。

你得说不出哪里出现EXC_BAD_ACCESS错误,使我们可以帮助你...

+0

他释放在'dealloc'据我可以看到标签.... – Mundi