字母plist与名称和描述numberOfRowsInSection问题 [英] Alphabetical plist with name and description numberOfRowsInSection issues

查看:106
本文介绍了字母plist与名称和描述numberOfRowsInSection问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个描述多种美食的plist。在我的plist中,我有26个数组,每个数组都有一个字母的字母键。在这些阵列中,包含多种美食,这些都是字典。在每个字典中都有2个字符串,一个用于烹饪名称,另一个用于描述菜肴。我非常接近在表视图控制器中实现它,但我遇到了一些问题。主要问题是访问名称并将其放在一个数组中,以便它可以用来告诉每个部分有多少行,该部分是字母表中的每个字母。我一直收到错误: - [_ NSArrayI长度]:无法识别的选择器发送到实例...

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...

非常感谢帮助。谢谢

这就是我所拥有的:

-(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;
    }

这里是我的字典看起来的图片:
< a href =http://imageshack.com/a/img835/9567/kkv7.png =nofollow> http://imageshack.com/a/img835/9567/kkv7.png

Also here is a picture of how my dictionary looks: http://imageshack.com/a/img835/9567/kkv7.png

这是我的搜索条形码。我需要将我所做的更改纳入我的plist。

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;
}


推荐答案

看起来像内容你从美食[key]得到的应该是一个数组,而不是字典。还不清楚你要用keyValues数组做什么。我认为你的方法可以简化为这个,

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];
        }
}

我假设您从使用呼叫中获取密钥allKeys在你的字典上,然后按字母顺序排序。如果是这样,那么你可以在cellForRowAtIndexPath中获取你的表视图的名称,

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;
}

这篇关于字母plist与名称和描述numberOfRowsInSection问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆