4

こんにちは、おそらく簡単な質問がありますが、まだ処理できません。

  • カテゴリ ID (12、23、56) を持つ配列を保持する Modelclass 'Location' があります。
  • 次に、利用可能なすべてのカテゴリ ID (1、2、3、4、5、6、7、...) を含む配列があります。
  • このすべてのカテゴリには ID があり、TableView に表示され、選択するかどうかを選択できます。
  • 前述の Filter TableView でのフィルターの選択に基づいて表示する必要がある MapView (Annotation Class is Location) にマーカーのパンチを表示します。

リスト内の選択に基づいて、すべてのマーカーを削除して再度追加する「カテゴリによるフィルター」機能を実装する必要があります。

したがって、配列とロケーション モデルのフィルター ID を、TableView のすべてのフィルター ID を含む配列と比較する必要があります。これには次の関数を使用しました。

for (Location *currLocation in arrListOfLocationsNearby) {

for (NSString *currKatId in currLocation.arrKatIds) {

NSInteger intCurrKatId = [currKatId integerValue];
NSLog(@"Current KatId %@ id: %d \n", currLocation.strAdr, intCurrKatId);

for (Filter *currFilter in [SDM getSharedDataManager].arrListOfFilterOptions) {

 if (currFilter.isSelected) {

  NSInteger intCurrFilterId = [currFilter.strAtt_id intValue];
  NSLog(@"Current Filter %@ id: %d \n", currFilter.strAtt_text, intCurrFilterId);

  if ([currKatId intValue] == [currFilter.strAtt_id intValue]) {
   currLocation.isVisible = YES;
  }else {
   currLocation.isVisible = NO;
  }
 }
}

} }

これがすべてをループする最も効果のない方法であることはわかっています。これに何らかの形で NSPredicate を使用したいのですが、これまで使用したことがなく、問題の例が見つかりません。

皆さんからのヒントはありますか?

よろしくm。

4

3 に答える 3

6

では、各「Location」オブジェクトには多くの「categoryID」がありますか?そして、 ""内にcategoryIDを持つLocationオブジェクトのいずれかを表示したいarrayOfEnabledCategoryIDsですか?

LocationエンティティとCategoryIDエンティティがあり、それらの間に多対多の関係()があると仮定すると、次のlocations <<--->> categoriesように実行できます。

NSArray * arrayOfEnabledCategoryIDs = ...; //these are the categories to display
NSFetchRequest * f = [[NSFetchRequest alloc] init];
[f setEntity:theEntityDescriptionForLocationObjects];
[f setPredicate:[NSPredicate predicateWithFormat:@"ANY categories.categoryID IN %@", arrayOfEnabledCategoryIDs]];
于 2010-06-26T03:41:57.973 に答える
0

Ruby On Rails の ActiveRecord に精通している場合。私はactiverecord+fetching+for+coreと呼ばれる優れたライブラリを使用しています。詳細については、http ://github.com/magicalpanda/activerecord-fetching-for-core-dataをご覧ください。

NSArray *departments = [NSArray arrayWithObjects:dept1, dept2, ..., nil];
NSPredicate *peopleFilter = [NSPredicate predicateWithFormat:@"Department IN %@", departments];

NSArray *people = [Person findAllWithPredicate:peopleFilter];

それは本当に私のコードをきれいにしました。

于 2010-09-15T08:18:22.063 に答える
0

各テストの文字列表現を試してください。

NSArray *arrayOfPredicateStrings = ;// array of all tests, probably [SDM sharedDataManager].arrListOfFilterOptions

NSPredicate *enabled = [NSPredicate predicateWithFormat:@"isSelected == true"];

NSArray *arrayOfEnabledPredicateStrings = 
        [arrayOfPredicateStrings filteredArrayUsingPredicate:enabled];

NSMutableString *formatString;
BOOL firstTime = YES;
for (NSString *string in arrayOfEnabledPredicateStrings) { 
    // Concatinate strings with OR inbetween
    if (firstTime) {
        firstTime = NO;
        formatString = [string mutableCopy];
    } else {
        [formatString appendFormat:@" OR %@", string]; 
    }
} // formatString should look something like @"ivar1 >= 1 OR ivar2 == true OR ivar3 != \"Hello, World\""
NSPredicate *predicate = [NSPredicate predicateWithFormat:string];

NSArray *arrListOfLocationsNearby; // assume already filled

NSArray *suitableNearbyLocations = 
        [arrListOfLocationsNearby filteredArrayUsingPrediacate:predicate];

CoreData データ ストアが SDM クラスの背後にある場合は、次のようにフェッチ リクエストを作成します。

NSPredicate *predicate = ;// get the predicate as above
NSManagedObjectContext  *context = ;// a managed object context
NSEntityDescription *entity = 
        [NSEntityDescription entityForName:@"entityName" inManagedObjectContext:context];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = entity;
request.predicate = predicate;

NSError *error;
NSArray *results = [context executeFetchRequest:request error:&error];
[request release];

述語形式の構文はここにあり、使用法はここにあります

于 2010-06-23T08:49:07.130 に答える