tableView:didSelectRowAtIndexPath - 发送到解除分配的实例的消息

时间:2013-04-07 15:54:07

标签: objective-c xcode uitableview didselectrowatindexpath

我已经阅读了很多主题,但我没有任何作用。 我有一个代码,但当我单击一个单元格时,我收到此错误: *** -[ModeViewController tableView:didSelectRowAtIndexPath:]: message sent to deallocated instance 0x764df40
代码:
ModeViewController.h

@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);
}

可能是什么问题?
提前谢谢。

2 个答案:

答案 0 :(得分:2)

  

- [ModeViewController tableView:didSelectRowAtIndexPath:]:发送到解除分配的实例0x764df40的消息

此消息并未说明“didSelectRowAtIndexPath:”存在问题,它表示您的“ModeViewController”实例存在问题,该方法在“didSelectRowAtIndexPath”中被解除分配(不再存在) :“正在被召唤。

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

您能说明控制器的创建和存储位置和方式吗?

答案 1 :(得分:0)

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