问题
So, I have a plist that describes multiple cuisines. In my plist, I have 26 arrays, each with a key of a letter of the alphabet. In those arrays, contain multiple cuisines, which are dictionaries. In every dictionary are 2 strings, one for the name of the cuisine and the other for the description of the cuisine. I am very close to implementing this inside a table view controller but I am having a few issues. The main issue is accessing the names and putting it in an array so that it can be used to tell how many rows are in each section, the section being each letter of the alphabet. I keep receiving the error :"-[_NSArrayI length]: unrecognized selector sent to instance...
Help would be very much appreciated. Thanks
This is what I have:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView.tag == 1){
NSString *key = keys[section]; //returns the key(letter) for the section
NSDictionary *content = cuisines[key]; //returns the dictionary that consists of name and description
NSString *name = [content valueForKey:@"name"];
NSMutableArray *keyValues = [@[] mutableCopy];
[keyValues addObject:name];
return [keyValues count];
}else{
return [filteredcuisines count];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if (tableView.tag == 1) {
return [keys count]; //Return the amount of letters for the number of sections
}else{
return 1;
}
Also here is a picture of how my dictionary looks: http://imageshack.com/a/img835/9567/kkv7.png
Here is my search bar code. I need to incorporate the changes I have made to my plist.
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
[filteredcuisines removeAllObjects];
if (searchString.length>0) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", self.searchBar.text];
for (NSString *key in keys) {
NSArray *matches = [cuisines[key] filteredArrayUsingPredicate:predicate];
[filteredcuisines addObjectsFromArray:matches];
}
}
return YES;
}
回答1:
It looks like "content" which you get from cuisines[key] should be an array, not a dictionary. It's also not clear what you're trying to do with the keyValues array. I think your method can be simplified to just this,
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (tableView.tag == 1){
NSString *key = keys[section];
NSArray *content = cuisines[key];
return content.count;
}else{
return [filteredcuisines count];
}
}
I'm assuming that you get keys from using calling allKeys on your dictionary, and then sorting that alphabetically. If that's so, then you can get the name for your table view in cellForRowAtIndexPath with this,
NSArray *arrayForSingleLetter = self.cuisines[keys[indexPath.section]];
cell.textLabel.text = arrayForSingleLetter[indexPath.row][@"name"];
Filtering your array will have to be modified to something like this,
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
[filteredcuisines removeAllObjects];
if (searchString.length>0) {
NSArray *arrayForSingleLetter = self.cuisines[[searchString substringToIndex:1]];
NSIndexSet *indxs = [arrayForSingleLetter indexesOfObjectsPassingTest:^BOOL(NSDictionary *dict, NSUInteger idx, BOOL *stop) {
return [dict[@"name"] rangeOfString:searchString].location != NSNotFound;
}];
[indxs enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
[filteredcuisines addObject:arrayForSingleLetter[idx][@"name"]];
}];
}
return YES;
}
来源:https://stackoverflow.com/questions/23917274/alphabetical-plist-with-name-and-description-numberofrowsinsection-issues