gpt4 book ai didi

objective-c - 详细信息 View 上的不同文本

转载 作者:行者123 更新时间:2023-11-29 04:16:59 25 4
gpt4 key购买 nike

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

在 cellForRowAtIndexPath 中提供正确的文本时:...我应该使用什么技术从该“网格”中选择正确的文本?开关结构相当大。有更简洁的解决方案吗?

最佳答案

我们可以使用 2 的幂来给出一些唯一的 key 。我们可以任意组合这些唯一的键,结果仍然是唯一的。 History of the Binary System

The fact that every number has a unique binary representation tells us that every number can be represented in a unique way as a sum of powers of 2. I wish to give an independent proof due to L. Euler (1707-1783) [Dunham, p 166] of the latter result.

对于代码:

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

关于objective-c - 详细信息 View 上的不同文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13563282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com