2013-04-07 47 views
1

我已经阅读了很多主题,但是我没有任何作用。 我有一个代码,但是当我点击一个单元格我得到这个错误: *** -[ModeViewController tableView:didSelectRowAtIndexPath:]: message sent to deallocated instance 0x764df40
代码:
ModeViewController.htableView:didSelectRowAtIndexPath - 发送到释放实例的消息

@interface ModeViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> 
@property (nonatomic, strong) UITableView *tableViewModeSelection; 

@end 

ModeViewController.m

@implementation ModeViewController 
@synthesize tableViewModeSelection; 

- (void)viewDidLoad 
{ 
tableViewModeSelection = [[UITableView alloc] initWithFrame:CGRectMake(10, 80, screenRect.size.width - 20, screenRect.size.height - 90) style:UITableViewStyleGrouped]; 
tableViewModeSelection.dataSource = self; 
tableViewModeSelection.delegate = self; 

[self.view addSubview:tableViewModeSelection]; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {  
static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
} 

cell.textLabel.text = [modes objectAtIndex:indexPath.row]; 
cell.backgroundView = [[UIView alloc] initWithFrame:CGRectZero]; 

return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
NSLog(@"Selected at row: %i", indexPath.row); 
} 

可能是什么问题?
在此先感谢。

+0

我不知道解决方案,但只是在一个侧面说明,'@interface WoLKiModeViewController:UIViewController ''和'@implementation ModeViewController'。他们不一定是相似的吗?我的意思是'@implementation WoLKiModeViewController' – Zen 2013-04-07 16:05:54

+0

现在它是相似的。我犯了一个错误。它必须是'ModeViewController'。 – 2013-04-07 17:01:14

回答

2

-[ModeViewController tableView:didSelectRowAtIndexPath:]: message sent to deallocated instance 0x764df40

此消息不说,还有一个问题“didSelectRowAtIndexPath方法:”它说是有你的“ModeViewController”,它被释放(不存在了)时的实例的问题正在调用“didSelectRowAtIndexPath:”方法。

所以问题是关于你的ModeViewController实例的生命期。

您可以显示您的控制器在哪里以及如何创建和存储?

+0

我在UIScrollView中有'ModeViewController'。所以它是较早建立的。这可能是问题吗?我该如何解决这个问题? – 2013-04-07 17:05:09

+0

@Luuksweb你如何设置它?错误提示没有人有强大的指向Controller的指针。顺便说一下,控制器不能进入UIScrollView,控制器不是视图。它的视图可以进入滚动视图,但不是控制器本身。也许这就是你混乱的根源。 – 2013-04-07 17:46:30

+0

这是正确的我从viewController添加视图,因为我不能添加viewController本身。我该如何解决这个问题? – 2013-04-27 15:54:46

0

也许你在scrollview中添加了ModeViewController的视图,但是释放了实际上是tableview委托的ModeViewController实例本身。所以例外即将到来。解决方案可能是在其视图存在之前不释放ModeViewController的实例。

+0

我使用ARC,所以我不能改变这一点。 – 2013-04-27 15:53:05

相关问题