I am writing an iPhone application that utilizes a UITableView within a UINavigationController on the front screen. I started a new project in Xcode using the Navigation Controller template, and added the following code to my RootViewController.h file:
And this code to my RootViewController.m UITableView dataSource methods:
However, when I scroll the tableView up, it crashes my application. I get an EXC_BAD_ACCESS error on the line:
It seems to me that the program crashes because it's looking for an object in the array that doesn't exist. I set my numberOfRowsInSection method and so I keep trying to figure out why the tableView is still trying to load data past that number.
This is almost identical to the format used by the book I'm following, and I've tried a few remedies. I've set a break point on RootViewController.m's dealloc method to make sure it wasn't being deallocated, and I've repeatedly checked variable values and memory allocation to try and diagnose the problem. I keep coming up with what I stated above, I just don't know how to fix it. Any help is greatly appreciated, and any constructive criticism as well. Thanks!
Code:
#import <UIKit/UIKit.h>
@interface RootViewController : UITableViewController {
NSArray *navOptions;
}
@end
And this code to my RootViewController.m UITableView dataSource methods:
Code:
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
navOptions = [NSArray arrayWithObjects:@"The|Ridge News", @"Small Groups", @"Trips", @"Merchandise", @"Events", @"Staff", nil];
return [navOptions count];
}
// Customize the appearance of table view cells.
- (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] autorelease];
}
[[cell textLabel] setText:[NSString stringWithFormat:@"%@", [navOptions objectAtIndex:indexPath.row]]];
return cell;
}
However, when I scroll the tableView up, it crashes my application. I get an EXC_BAD_ACCESS error on the line:
Code:
[[cell textLabel] setText:[NSString stringWithFormat:@"%@", [navOptions objectAtIndex:indexPath.row]]];
It seems to me that the program crashes because it's looking for an object in the array that doesn't exist. I set my numberOfRowsInSection method and so I keep trying to figure out why the tableView is still trying to load data past that number.
This is almost identical to the format used by the book I'm following, and I've tried a few remedies. I've set a break point on RootViewController.m's dealloc method to make sure it wasn't being deallocated, and I've repeatedly checked variable values and memory allocation to try and diagnose the problem. I keep coming up with what I stated above, I just don't know how to fix it. Any help is greatly appreciated, and any constructive criticism as well. Thanks!