I am trying to set up my uitableview for indexing, I have got number of sections working fine, now and the data is in the NSDictionary heres my output with nslog —>
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 am now trying to set up my table like so
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return [arraysByLetter count];
//return 0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [[arraysByLetter objectAtIndex:section] count];
//return 1;
}
however I am getting a warning ‘NSMutableDictionary’ may not respond to ‘objectAtIndex:’ inside my numberOfRowsInSection delegate method… How do I get around this?
You cannot index a dictionary as you would an array. You access dictionaries using keys. NSDictionary does provide two methods for getting arrays: allKeys and allValues.
I’m not sure what you are trying to accomplish, but if you want to organize the car makers alphabetically, you can get all the keys ([arraysByLetter allKeys]), sort them alphabetically, and then index into that sorted array. When you actually get the car makers’ names you will then use objectForKey: to load the array of makers.
Update:
Assuming a sorted array named sortedLetters, you can change your numberOfRowsInSection to the following:
You probably want to setup the sortedLetters array on initialization, unless it is dynamic.