Reputation: 132
I have a problem that for some reason I cannot figure out on my own.
What i have is a tableView that gets loaded from an array, that is loaded from a file. (I declared my arrays as NSArray in the header file)
NSString *subMenuFileList = [[NSBundle mainBundle] pathForResource:@"myfile" ofType:@"plist"]; // file list name/location
sectionsArray = [[NSArray alloc] initWithContentsOfFile:subMenuFileList];//loading my array with the contents of the file
I have everything working fine, for my table sections i use sectionsArray.count
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return sectionsArray.count; // return number of rows
and for the cell.textLabel.text i have the following code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell.textLabel.text= [sectionsArray objectAtIndex:indexPath.row];// get cell name from the array at row#
return cell;
also works fine.
The problem that i have is that the file that is loaded is not alphabetically arranged so my array turns out to be not alphabetically arranged. My solution to the problem was to load a file into a temporary array, sort it and then assign it to the sections array with the following simple code:
NSString *subMenuFileList = [[NSBundle mainBundle] pathForResource:@"myfile" ofType:@"plist"]; // file list name/location
tempArray = [[NSArray alloc] initWithContentsOfFile:subMenuFileList];
sectionsArray = [tempArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];// my new sorted array
for some reason after this simple tweek, none of the other functions can access i get EXC_BAD ACCESS when the program tries to determine number of rows in the section or the cell textLabel see the picture files ( http://img846.imageshack.us/i/screenshot20110416at141.png/ ). I cannot figure out why that happens, since the variables are global and when i do NSLog for sectionsArray.count it does print proper values.
Any help would be really appreciated. Thank you!!
Upvotes: 0
Views: 294
Reputation: 3588
sectionsArray = [tempArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
this will result into an autoreleased array. you may retain it.
sectionsArray = [[tempArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)] retain];
In general I recommend to use a property for your sectionsarray (nonatomic, retain).. then this is your code:
NSArray* tempArray = [[NSArray alloc] initWithContentsOfFile:subMenuFileList];
self.sectionsArray = [tempArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
[tempArray release];
Best Regards, Christian
Upvotes: 1