0

さて、テーブルビューに移動するときに、サイドにアクセサリの目盛りを配置できるように、NSArray値から indexPath 値を作成しようとしています。NSDictionary

私はこのように見えるNSArrayのを持っていますNSDictionaries

{
        HAS = 1;
        ISL = 1;
        ISV = 0;
        MAN = BANKING;
        MON = 324;
}

したがって、配列には、それぞれ異なる値を持つ約 80 個のエントリが含まれます。

IndexPath を作成するには、MAN == selected Stringになるまで、 MANの一意の最初の文字の数を数える必要があります。これにより、セクション番号が得られます。

次に、選択した行のセクション タイプに含まれるエントリの数をカウントする必要があります。

ここからすでにいくつかの助けがありましたが、ロジックの欠陥を特定できませんでした..選択したセクションの開始からではなく、すべての全体をカウントしていた行については、行数が完全に詰まっていました. 以下は、私が達成しようとしていることの例です。

a, aa, b, c, d, dd, ddd, dddd, e, f, g, gg

ユーザーが ddd を選択すると、インデックスパスは次のようになります。

セクション

a, b, c, (d), e, f, g = 3

d, dd, (ddd), dddd = 2

私はそのようにセクションをうまく取得できます

NSArray *MANArray = [sortedArray valueForKey:@"MAN"];

NSMutableArray *letters = [NSMutableArray array];
NSString *currentLetter = nil;
for (NSString *string in MANArray) {
    if (string.length > 0) {
        NSString *letter = [string substringToIndex:1];
        if (![letter isEqualToString:currentLetter]) {
            [letters addObject:letter];
            currentLetter = letter;
        }
    }
}
NSArray *firstLetterArray = [NSArray arrayWithArray:letters];
int sectionCount = 0;
for(int i=0; i<[firstLetterArray count]; i++){
    if( [[firstLetterArray objectAtIndex:i] isEqualToString:completedStringLetter]){
        sectionCount = i;
        NSLog(@"%d", sectionCount);
    }
}

しかし、選択したセクションから行数を取得しようとすると、行き詰まります。

どんな助けでも大歓迎です。

アップデート:

これは、選択した値までカウントするために行ったことですが、最初の文字に応じてセクションから開始する必要があるため、正しくないと言いました。

NSNumber * myNumber = [NSNumber numberWithInteger:[modIdString integerValue]];

//                
NSArray *subarray = [sortedArray valueForKey:@"MOD"];
for(int i=0; i<[subarray count]; i++){
   if( [[subarray objectAtIndex:i] isEqualToNumber:myNumber]){
      //you have found it, do whatever you need, and break from the loop
      modelResultIndexPath = nil;
      modelResultIndexPath = [NSIndexPath indexPathForRow:sectionCount inSection:i];
   }
}
4

3 に答える 3

2

"a, aa, b, c, d, dd, ddd, dddd, e, f, g, gg"値を持つNSDictionary配列を次のような配列に変換するのはどうですか?

dict = {
        a = [a, aa];
        b = [b];
        c = [c];
        d = [d, dd, ddd, dddd];
        e = [e];
        f = [f];
        g = [g, gg];
}

次に[[dict allKeys] count]、セクション数と[[dict valueForKey:@"d"] count]対応する行数を指定する必要があります。

配列を辞書の配列に変換するには、このコードを使用します。

NSArray *sortedArray = [arrayData sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSMutableDictionary *arraysByLetterDict = [NSMutableDictionary dictionary];

for (NSString *value in sortedArray) {
    NSString *firstLetter = [value substringWithRange:NSMakeRange(0, 1)];
    NSMutableArray *arrayForLetter = [arraysByLetterDict objectForKey:firstLetter];
    if (!arrayForLetter) {
        arrayForLetter = [NSMutableArray array];
        [arraysByLetterDict setObject:arrayForLetter forKey:firstLetter];
    }
    [arrayForLetter addObject:value];
}
NSLog(@"Dictionary: %@", arraysByLetterDict);

インデックスパスは、次のように計算できます。

NSArray *allKeys = [[dict allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
int totalSectionCount = [allKeys count];
int totalRowCount = [[dict valueForKey:@"d"] count];

int sectionCount = [allKeys indexOfObject:@"d"];
int rowCount = [[dict valueForKey:@"d"] indexOfObject:@"ddd"];

modelResultIndexPath = [NSIndexPath indexPathForRow:rowCount inSection:sectionCount];
于 2012-11-07T04:22:19.140 に答える
0

辞書の元の配列に対して述語を使用して、特定の文字で始まるすべての辞書を除外し、idexOfObjectPassingTest を使用して探している一致する辞書のインデックスを見つけることができます。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.MAN beginswith[c] '%@'", SELECTED_FIRST_LETTER_OF_MAN];
NSArray *filteredDictionaries = [sortedArray filteredArrayUsingPredicate:predicate];
int rowCount = [filteredDictionaries indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
    NSDictionary *dictionary = (NSDictionary *)obj;

    if ([dictionary[@"MAN"] isEqualToString:SELECTED_MAN]
        && [dictionary[@"MON"] isEqualToString:SELECTED_MON]) {
        return YES;
    } else {
        return NO;
    }
}];
于 2012-11-07T02:53:40.377 に答える