1

tableViewsと辞書は初めてですが、問題があります。ViewDidLoadで、多くのMutableArrayを初期化し、NSDictionaryを使用してデータを追加しています。例:

- (void)viewDidLoad {
nomosXiou=[[NSMutableArray alloc] init];

[nomosXiou addObject:[[NSDictionary alloc] initWithObjectsAndKeys:@"Mary",@"name",@"USA",@"country", nil]];
[nomosXiou addObject:[[NSDictionary alloc] initWithObjectsAndKeys:@"Peter",@"name",@"Germany",@"country", nil]]; 

[super viewDidLoad];
// Do any additional setup after loading the view.}

以前のViewControllerでは、ユーザーは国を選択します。その選択に基づいて、他のすべてのエントリを配列から削除するにはどうすればよいですか?

前もって感謝します...

4

2 に答える 2

2

まず、コードフラグメントにエラーがあることに注意してください。それは読むべきです:

NSMutableArray *nomosXiou= [[NSMutableArray alloc] init];

やりたいことを行う方法はいくつかありますが、最も簡単な方法はおそらく次のとおりです。

NSString *countryName;    // You picked this in another view controller
NSMutableArray *newNomosXiou= [[NSMutableArray alloc] init];

for (NSDictionary *entry in nomosXiou) {
    if ([[entry objectForKey:@"country"] isEqualToString:countryName])
        [newNomosXiou addObject:entry];
}

これが行われると、で設定された国からのnewNomosXiouエントリのみが含まれます。nomosXioucountryName

于 2012-07-28T21:27:59.897 に答える
0

このような何かが仕事をします:

NSMutableArray *nomosXiou = [[NSMutableArray alloc] init];
NSString *country = @"Germany"; // This is what you got from previous controller

// Some test data. Here we will eventually keep only countries == Germany
[nomosXiou addObject:[[NSDictionary alloc] initWithObjectsAndKeys:@"Mary",@"name",@"USA",@"country", nil]];
[nomosXiou addObject:[[NSDictionary alloc] initWithObjectsAndKeys:@"Peter",@"name",@"Germany",@"country", nil]];
[nomosXiou addObject:[[NSDictionary alloc] initWithObjectsAndKeys:@"George",@"name",@"Germany",@"country", nil]];

// Here we'll keep track of all the objects passing our test
// i.e. they are not equal to our 'country' string
NSIndexSet *indexset = [nomosXiou indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop){
    return (BOOL)![[obj valueForKey:@"country"] isEqualToString:country];
    }];

// Finally we remove the objects from our array
[nomosXiou removeObjectsAtIndexes:indexset];
于 2012-07-28T22:46:07.563 に答える