0

アイテムを含むテーブルビューがあります。アイテムをクリックすると、詳細ビ​​ューが表示されます。各アイテムには、意味のある状態を表す 2 つの列挙状態があります。最初の列挙には 6 つの異なる値があり、2 番目の列挙には 5 つの異なる値があります。これにより、30の組み合わせが得られます。組み合わせごとに、一意のテキストが必要です。

cellForRowAtIndexPath:...で正しいテキストを提供する場合、その「グリッド」から正しいテキストを選択するにはどのようなテクニックを使用すればよいですか? スイッチ構造はかなり大きいです。もっときちんとした解決策はありますか?

4

1 に答える 1

1

2の累乗を使用して、いくつかの一意のキーを与えることができます。そして、これらの一意のキーを任意に組み合わせることができ、結果は引き続き一意になります。 バイナリシステムの歴史

すべての数が一意の2進表現を持っているという事実は、すべての数が2の累乗の合計として一意の方法で表現できることを示しています。L。オイラー(1707-1783)[Dunham、後者の結果のp166]。

コードの場合:

typedef enum {
    FirstTypeOne = 1 << 0,
    FirstTypeTwo = 1 << 1,
    FirstTypeThree = 1 << 2,
    FirstTypeFour = 1 << 3,
    FirstTypeFive = 1 << 4,
    FirstTypeSix = 1 << 5
} FirstType;

typedef enum {
    SecondTypeSeven = 1 << 6,
    SecondTypeEight = 1 << 7,
    SecondTypeNine = 1 << 8,
    SecondTypeTen = 1 << 9,
    SecondTypeEleven = 1 << 10
} SecondType ;

const int FirstTypeCount = 6;
const int SecondTypeCount = 5;

// First create two array, each containing one of the corresponding enum value.
NSMutableArray *firstTypeArray = [NSMutableArray arrayWithCapacity:FirstTypeCount];
NSMutableArray *secondTypeArray = [NSMutableArray arrayWithCapacity:SecondTypeCount];

for (int i=0; i<FirstTypeCount; ++i) {
    [firstTypeArray addObject:[NSNumber numberWithInt:1<<i]];
}
for (int i=0; i<SecondTypeCount; ++i) {
    [secondTypeArray addObject:[NSNumber numberWithInt:1<<(i+FirstTypeCount)]];
}

// Then compute an array which contains the unique keys.
// Here if we use 
NSMutableArray *keysArray = [NSMutableArray arrayWithCapacity:FirstTypeCount * SecondTypeCount];
for (NSNumber *firstTypeKey in firstTypeArray) {
    for (NSNumber *secondTypeKey in secondTypeArray) {
        int uniqueKey = [firstTypeKey intValue] + [secondTypeKey intValue];
        [keysArray addObject:[NSNumber numberWithInt:uniqueKey]];
    }
}

// Keep the keys asending.
[keysArray sortUsingComparator:^(NSNumber *a, NSNumber *b){
    return [a compare:b];
}];

// Here you need to put your keys.
NSMutableArray *uniqueTextArray = [NSMutableArray arrayWithCapacity:keysArray.count];
for (int i=0; i<keysArray.count; ++i) {
    [uniqueTextArray addObject:[NSString stringWithFormat:@"%i text", i]];
}

// Dictionary with unique keys and unique text.
NSDictionary *textDic = [NSDictionary dictionaryWithObjects:uniqueTextArray forKeys:keysArray];

// Here you can use (FirstType + SecondType) as key.
// Bellow is two test demo.
NSNumber *key = [NSNumber numberWithInt:FirstTypeOne + SecondTypeSeven];
NSLog(@"text %@ for uniquekey %@", [textDic objectForKey:key], key);
key = [NSNumber numberWithInt:FirstTypeThree + SecondTypeNine];
NSLog(@"text %@ for uniquekey %@", [textDic objectForKey:key], key);
于 2012-11-26T12:13:53.333 に答える