Eric
Eric

Reputation: 1014

UITableView crash on scroll or tap - issue with ARC?

I am trying to create a simple UItableview app in a project utilizing ARC. The table renders just fine but if I try to scroll or tap a cell the app crashes.

Looking at the NSZombies (is that the proper way to say that?) I get the message "-[PlacesViewController respondsToSelector:]: message sent to deallocated instance 0x7c29240"

I believe this has something to do with ARC as I have successfully implemented UItableviews in the past but this is my first project using ARC. I know I must be missing something very simple.

PlacesTableViewController.h

@interface PlacesViewController : UIViewController
<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *myTableView;

@end 

PlacesTableViewController.m

#import "PlacesTableViewController.h"

@implementation PlacesViewController

@synthesize myTableView;
- (void)viewDidLoad
{
    [super viewDidLoad];

    self.myTableView    =   [[UITableView alloc] initWithFrame:self.view.bounds   style:UITableViewStylePlain];

    self.myTableView.dataSource =   self;
    self.myTableView.delegate   =   self;

    [self.view addSubview:self.myTableView];
}
#pragma mark - UIViewTable DataSource methods

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 100;
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath  *)indexPath
{
    UITableViewCell *result = nil;

    static NSString *CellIdentifier = @"MyTableViewCellId";

    result =    [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(result == nil)
    {
        result = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    result.textLabel.text   =   [NSString stringWithFormat:@"Cell %ld",(long)indexPath.row];


    return result;
}
@end

Upvotes: 4

Views: 451

Answers (1)

amattn
amattn

Reputation: 10065

There is nothing obviously wrong with the code you posted. The problem is with the code that creates and holds onto PlacesViewController. You are probably creating it but not storing it anywhere permanent. Your PlacesViewController needs to be saved into an ivar or put in a view container that will manage it for you (UINavigationController, UITabController or similar)

Upvotes: 1

Related Questions