2012-11-26 89 views
0

我有一個包含項目的表格視圖。如果我點擊一個項目,它會顯示詳細視圖。 現在每個項目都有兩個代表有意義狀態的枚舉狀態。第一個枚舉有6個不同的值,第二個枚舉可以有5個不同的值。這給了我30個組合。 對於每個組合,我需要一個獨特的文字。在詳細視圖上變化的文本

當在cellForRowAtIndexPath中提供正確的文本時:...我應該使用什麼技術從該「網格」中選擇正確的文本? 開關結構相當大。有沒有更好的解決方案?

+0

請發表您的代碼 –

+0

我想知道寫什麼,所以很明顯沒有代碼呢。但即使有,這也是一個通用的問題,也許有一個語言特定的解決方案。將這個嵌套開關項解析爲兩個枚舉有什麼用處? –

回答

1

我們可以使用2的冪給出一些唯一的密鑰。我們可以任意組合這些獨特的鍵,結果仍然是唯一的。 History of the Binary System

,每一個號碼都有唯一的二進制表示的事實告訴我們 ,每個號碼可以以獨特的方式來表示的2 功率的總和我想給一個獨立的證明,由於到L Euler (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);