Reputation: 71
I'm trying to load my NSMutableArray into a UITableView but it crashes as soon as you scroll. The data loads into the UITableView but like I said I can't scroll.
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize myArray = _myArray;
#pragma mark TableViewStuff
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _myArray.count;
}
//—-insert individual row into the table view—-
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
//—-try to get a reusable cell—-
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//—-create new cell if no reusable cell is available—-
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier]
autorelease];
}
//—-set the text to display for the cell—-
NSString *cellValue = [_myArray objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
#pragma mark LifeCycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// Load the file into a string
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"listOfColleges" ofType:@"txt"];
NSString* myString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
//Fill the array with subsets of the string
_myArray = [NSMutableArray arrayWithArray:[myString componentsSeparatedByString:@"\n"]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)dealloc
{
[_myArray release];
[super dealloc];
}
@end
MyArray is retained and it is non-atomic, so I should be good to go. Maybe something is dying though before the UITableView can use it?
The error that I am getting is as follows:
EXC_BAD_ACCESS @ this line - NSString *cellValue = [_myArray objectAtIndex:indexPath.row];
Upvotes: 0
Views: 224
Reputation: 16124
The problem is this:
_myArray = [NSMutableArray arrayWithArray:[myString componentsSeparatedByString:@"\n"]];
You are not accessing your setter so the retain isn't happening. You want:
self._myArray = [NSMutableArray arrayWithArray:[myString componentsSeparatedByString:@"\n"]];
or
[_myArray release];
_myArray = [[NSMutableArray arrayWithArray:[myString componentsSeparatedByString:@"\n"]] retain];
or
[_myArray release];
_myArray = [[NSMutableArray alloc] initWithArray:[myString componentsSeparatedByString:@"\n"]];
Upvotes: 5