【发布时间】:2013-07-07 19:56:26
【问题描述】:
为了正确传达作者的意图,已重新编写此问题
我有一个 MasterDetail 应用程序,它最终将成为一种通过固定数量的“挑战”(准确地说是 60 个)跟踪“玩家”数量的方法。每个玩家的挑战都是相同的,而每个玩家在个人挑战中的状态可能会有所不同。所有关于玩家的数据都存储在 Postgres 数据库中,然后使用 LeftViewController 中的 JSONModel 获取。我从数据库中正确获取了所有数据,并且已成功将其放入 NSMutableDictionary。 Dictionary 中的数据如下所示(这是一位玩家):
player = (
{
id = 9;
name = "Arthur Dent";
currentChallenge = "Cooking";
timeStarted = "04 Jul 2013 1:08 PM";
challengesDone = (
{
challengeName = "Flying";
timeCompleted = "04 Jul 2013 12:08 PM";
}
);
nextChallenge = "Swimming";
freePass = (
Running,
"Climbing"
);
},
正如你所看到的,玩家有一些挑战“完成”(“done”),一些他不需要做(“pass”),一个他正在做(“currentChallenge”)和一个他接下来要做的挑战(“nextChallenge”)。对于玩家可能处于的这些状态中的每一个,我希望 RightViewController 中的“挑战”进行颜色编码,以便您一眼就能了解正在发生的事情。我需要“挑战”来根据玩家的状态点亮不同的颜色,这取决于我的 NSMutableDictionary 中的数据。
通过从 NSDictionary 中的数据创建数组,我可以通过这样做来改变颜色:
if ([freePassArray containsObject:@"challenge three name"])
{
UIImage *image = [UIImage imageNamed:@"status_pass.png"]; //status_pass.png is my color
[challengeThreeImageView setImage:image];
}
但如果我这样做,我将不得不编写 240 个 if 和 else if 语句来涵盖所有 60 个挑战的所有可能状态。
我的数组只包含所选玩家的数据,因为我像这样传递数据:
*LeftViewController.m * didSelectRowAtIndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//Re-fetch the feed from the Postgres Database when a user selects an entry
[JSONHTTPClient getJSONFromURLWithString:@"http://myurl" completion:^(NSDictionary *json, JSONModelError *err) {
NSError* error = nil;
_feed = [[PostgresFeed alloc] initWithDictionary:json error:&error];
[[NSNotificationCenter defaultCenter] postNotificationName:@"myNotification" object:nil userInfo:[[json objectForKey:@"preclear"] objectAtIndex:indexPath.row]];
}];
Player *selectedPlayer = [_players objectAtIndex:indexPath.row];
if (_delegate)
{
[_delegate selectedPlayer:selectedPlayer];
}
}
因此图像不会同时为所有玩家设置,但在选择另一个玩家后它们也不会清除。
如何在不编写 240 if 和 else if 语句的情况下改变颜色?当我选择另一个播放器时如何清除颜色?
【问题讨论】:
-
澄清一下,您有一个玩家列表,在详细视图中您有所有 50 个挑战。对于每个用户选择,您想查看 50 个挑战中的哪些已完成,免费、当前和通过。对吗?
-
@Vame 是的,我想我可以这样说。你是对的。
-
我很困惑——这与 JSON 有什么关系?您有想要用来控制某些视图组件状态的值。值的来源无关紧要。
-
@HotLicks 你是对的,JSON 标签已被删除。对此感到抱歉。
-
通常大约 80% 的解决问题是知道问题是什么。
标签: ios objective-c ipad uiimageview