Reputation: 45
I have read much topics, but I nothing works.
I have a code, but when I click a cell I get this error:
*** -[ModeViewController tableView:didSelectRowAtIndexPath:]: message sent to deallocated instance 0x764df40
Code:
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);
}
What could be the problem?
Thanks in advance.
Upvotes: 1
Views: 1351
Reputation: 56
Perhaps you are adding ModeViewController's view on the scrollview, but releasing the ModeViewController instance itself which is actually the tableview's delegate. So the exception is coming. Solution could be to not release the ModeViewController's instance until the its view is existing.
Upvotes: 0
Reputation: 8664
-[ModeViewController tableView:didSelectRowAtIndexPath:]: message sent to deallocated instance 0x764df40
This message doesn't say that there is a problem with "didSelectRowAtIndexPath:", it saying that there is a problem with your instance of "ModeViewController", which is deallocated (don't exist anymore) when the method "didSelectRowAtIndexPath:" is being called.
So the problem is about the life time of your ModeViewController instance.
Can you show where and how your controller is created and stored?
Upvotes: 2