【问题标题】:tableView:didSelectRowAtIndexPath - message sent to deallocated instancetableView:didSelectRowAtIndexPath - 发送到已释放实例的消息
【发布时间】:2013-04-07 15:54:07
【问题描述】:

我已经阅读了很多主题,但我没有任何工作。 我有一个代码,但是当我单击一个单元格时,我收到此错误: *** -[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);
}

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

【问题讨论】:

  • 我不知道解决方案,但只是附带说明,@interface WoLKiModeViewController : UIViewController &lt;UITableViewDelegate, UITableViewDataSource&gt;@implementation ModeViewController。他们不必相似吗?我的意思是@implementation WoLKiModeViewController
  • 现在类似。我犯了一个错误。必须是ModeViewController

标签: objective-c xcode uitableview didselectrowatindexpath


【解决方案1】:

-[ModeViewController tableView:didSelectRowAtIndexPath:]: 消息发送到deallocated instance 0x764df40

此消息并不是说“didSelectRowAtIndexPath:”有问题,而是说您的“ModeViewController”实例有问题,当方法“didSelectRowAtIndexPath”时,该实例被释放(不再存在) :" 正在被调用。

所以问题在于您的 ModeViewController 实例的生命周期。

您能否说明您的控制器是在何处以及如何创建和存储的?

【讨论】:

  • 我在 UIScrollView 中有 ModeViewController。所以设置得比较早。这可能是问题吗?我该如何解决这个问题?
  • @Luuksweb 你是怎么设置的?该错误表明没有人拥有指向控制器的强指针。顺便说一句,Controller 不能进入 UIScrollView,Controller 不是 View。它的视图可以进入滚动视图,但不能进入控制器本身。也许这就是你困惑的根源。
  • 我从viewController添加视图是对的,因为我无法添加viewController本身。我该如何解决这个问题?
  • @Luuksweb,这个问题仍然存在:“你能展示一下你的控制器是在哪里以及如何创建和存储的吗?”,没有人愿意为你的视图控制器提供一个强指针。
【解决方案2】:

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

【讨论】:

  • 我使用 ARC,所以无法更改。
猜你喜欢
  • 2011-06-16
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多