Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITableView crashes on scroll

I have a TableView that builds and draws ok, but then crashes on scrolling the view. I've run through the debugger and it appears that my class level variables are being overwritten somehow so they no longer exist when the titleForHeaderInSection is being called again. The very strange thing is that if I replace the code:

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *sectionTitle = [favouritesDataSections objectAtIndex:section];
return sectionTitle;
}

with:

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *sectionTitle = @"Test";
return sectionTitle;
}

It still crashes but this time the debugger lists not an NSString when you hover over the sectionTitle variable.

This is the code I used to create the view and set up the class level variables:

- (void)loadView {
[super loadView];
CGRect tableSize = CGRectMake(0,0,320,460);
UITableView *favouritesTableView = [[UITableView alloc] initWithFrame:tableSize style:UITableViewStylePlain];
favouritesTableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
favouritesTableView.dataSource = self;
favouritesTableView.delegate = self;
favouritesTableView.rowHeight = 52;
[self.view addSubview:favouritesTableView];
}

- (void)viewDidLoad {
[super viewDidLoad];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Get the full path of the favourites plist
NSString *filename = [documentsDirectory stringByAppendingPathComponent:@"Favourites.plist"];
// Initialise Dictionary and array
favouritesDataAll = [[NSMutableDictionary alloc] init];
favouritesDataSections = [[NSArray alloc] init];

NSDictionary *dict = [[[NSMutableDictionary alloc] initWithContentsOfFile:filename] retain];
favouritesDataAll = dict;
[dict release];

favouritesDataSections = [favouritesDataAll allKeys];   
}

I am going absolutely mad trying to track this down - spent 2 days on it so far so would be externally grateful for any help.

Best regards

Dave

like image 265
Magic Bullet Dave Avatar asked Nov 29 '09 08:11

Magic Bullet Dave


2 Answers

OK, fixed it... changed

favouritesDataSections = [favouritesDataAll allKeys];

To:

favouritesDataSections = [[favouritesDataAll allKeys] retain];

And it all appears to work. From this I would deduce that the array I was using to store the section headings was pointing to data that was autoreleased at some random point, which is why it was barfing at seemingly odd places.

I do admit though that I am still at the "trial and error" stage of coding and don't fully understand what I am doing (I am sure you will be cringing reading this). It would be useful to me if you have any thoughts/comments links to further reading or posts about how all this is working (i.e., when and why to use retain, etc) to further my understanding.

Thanks again, Dave

like image 55
Magic Bullet Dave Avatar answered Sep 22 '22 18:09

Magic Bullet Dave


I would recommend using the @property setters to avoid this problem, the array is autoreleased, so you called a manual retain on it, this fixed the problem, but a simpler fix would be to use:

self.favoritesDataSection

This is automated by @property (retain) retain means retain is called when this is set, and release when it is set to nil or a different object.

like image 39
Alex Gosselin Avatar answered Sep 23 '22 18:09

Alex Gosselin