详细信息视图上的文本变化



我有一个包含项目的表格视图。如果我单击某个项目,则会显示其详细信息视图。现在,每个项目都有两个表示有意义状态的枚举状态。第一个枚举有 6 个不同的值,第二个枚举可以有 5 个不同的值。这给了我 30 种组合。对于每个组合,我都需要一个唯一的文本。

在单元格中提供正确的文本时,为RowAtIndexPath:...我应该使用什么技术从该"网格"中选择正确的文本?开关结构相当大。有没有更简洁的解决方案?

我们可以用 2 的幂来给出一些唯一的键。我们可以任意组合这些唯一的键,结果仍然是唯一的。二进制系统的历史

每个数字都有唯一的二进制表示的事实告诉我们 每个数字都可以以独特的方式表示为 2的幂。我想给出一个独立的证明,因为L.欧拉 (1707-1783)[邓纳姆,第166页]的后一个结果。

对于代码:

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);

最新更新