编辑:好的,所以我想出了如何解决我原来的问题,但我不确定这是否是最好的方法。
我的新问题是,假设我有一个 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 中连接。我需要它们可供课堂使用,因为我需要知道何时按下按钮并能够更改文本。当我运行该应用程序时,我开始滚动并在几秒钟后崩溃,主要是 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