Cannot access NSMutableDictionary outside method where values are passed into it
I am trying to set up Index and sections for my uitableview, which I have managed to do.. However now that I am trying to pass my NSDictionary values over to my uitableviewcell my app is crashing when I try to access the NSDictionary outside of the method that I passed the values to it from.
I am thinking that maybe I am not passing the values in correctly or something along the line of that, but I simply cannot figure out why its going this...
Heres my code...
.h
@interface VehicleResultViewController : UITableViewController <NSXMLParserDelegate> {
//......
//Indexed tableview stuff
NSArray *sortedArray;
NSMutableDictionary *arraysByLetter;
NSMutableArray *sectionLetters;
}
//.....
//Indexed tableview stuff
@property (nonatomic, retain) IBOutlet NSArray *sortedArray;
@property (nonatomic, retain) IBOutlet NSMutableDictionary *arraysByLetter;
@property (nonatomic, retain) IBOutlet NSMutableArray *sectionLetters;
//....
.m
//...
//This is where I try to access t开发者_JAVA百科he NSDictionary to pass it to my uitableviewcells
- (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.accessoryType = UITableViewCellAccessoryNone; //make sure their are no tickes in the tableview
cell.selectionStyle = UITableViewCellSelectionStyleNone; // no blue selection
// Configure the cell...
NSString *value = [self.arraysByLetter objectForKey:[[self.arraysByLetter allKeys] objectAtIndex:indexPath.row]];
cell.textLabel.text = key;
NSLog(@"%@",arraysByLetter);
return cell;
}
//This is where I set NSDictionary
//method to sort array and split for use with uitableview Index
- (IBAction)startSortingTheArray:(NSMutableArray *)arrayData
{
//Sort incoming array alphabetically
sortedArray = [arrayData sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
//NSLog(@"%@",sortedArray);
// Dictionary will hold our sub-arrays
arraysByLetter = [NSMutableDictionary dictionary];
sectionLetters = [[NSMutableArray alloc] init];
// Iterate over all the values in our sorted array
for (NSString *value in sortedArray) {
// Get the first letter and its associated array from the dictionary.
// If the dictionary does not exist create one and associate it with the letter.
NSString *firstLetter = [value substringWithRange:NSMakeRange(0, 1)];
NSMutableArray *arrayForLetter = [arraysByLetter objectForKey:firstLetter];
if (arrayForLetter == nil) {
arrayForLetter = [NSMutableArray array];
[arraysByLetter setObject:arrayForLetter forKey:firstLetter];
[sectionLetters addObject:firstLetter]; // This will be used to set index and section titles
}
// Add the value to the array for this letter
[arrayForLetter addObject:value];
}
// arraysByLetter will contain the result you expect
NSLog(@"Dictionary: %@", arraysByLetter); //This prints what is currently in the NSDictionary
//Reloads data in table
[self.tableView reloadData];
}
.output of checking the Dictionary with NSLog in the last method above
Dictionary: {
H = (
Honda,
Honda,
Honda,
Honda,
Honda,
Honda,
Honda
);
M = (
Mazda,
Mazda,
Mitsubishi,
Mitsubishi,
Mitsubishi,
Mitsubishi,
Mitsubishi,
Mitsubishi
);
N = (
Nissan,
Nissan,
Nissan,
Nissan,
Nissan,
Nissan,
Nissan
);
T = (
Toyota,
Toyota,
Toyota
);
}
I have debugged the two points (where i set the NSdictionary in the method) and (where I access the NSDictionary in cellforrowatindexpath) and it is defiantly set before I even try to use it.
Any help would be greatly appreciated..
//Indexed tableview stuff
@property (nonatomic, retain) NSArray *sortedArray;
@property (nonatomic, retain) NSMutableDictionary *arraysByLetter;
@property (nonatomic, retain) NSMutableArray *sectionLetters;
REMOVE IBOutlet from properties declarations. It's only for Interface Builder controls.
Also correct dictonary allocation - self.arraysByLetter = [NSMutableDictionary dictionary];
The NSMutableDictionary
that is allocated is autoreleased
, therefore when it is called in the other method the NSAutoreleasePool
has been drained, and the NSMutableDictionary
has been released
. If you want to retain the object using the property you have to do it like this:
self.arraysByLetter = [NSMutableDictionary dictionary];
self
will set the dictionary using the setter which is declared as retain
, so it will be available when you try to use it later on.
As a note any method that does not start with new or alloc or contains copy must return an autoreleased
object, which is your case.
精彩评论