将多个图像添加到哪种数组的最佳方法?
我正在为字母表中的所有字母创建可拖动的字母图块。我为每个字母创建 5 个,以防一个单词需要多个相同的字母。
我正在 MainViewControllers ViewDidLoad: 方法中以编程方式创建每个 UIImageView。
- (void)ViewDidLoad:
{
for (int i = 1; i <= 5; i++)
{
letterA = [[LetterTiles alloc] initWithFrame:CGRectMake(39, 104, 70, 70)];
[letterA setImage:[UIImage imageNamed:@"a.png"]];
letterA.tag = i;
[letterA setUserInteractionEnabled:YES];
[self.view addSubview:letterA];
[letterA release];
for (int i = 1; i <= 5; i++)
{
letterB = [[LetterTiles alloc] initWithFrame:CGRectMake(112, 104, 70, 70)];
[letterB setImage:[UIImage imageNamed:@"b.png"]];
letterB.tag = i;
[letterB setUserInteractionEnabled:YES];
[self.view addSubview:letterB];
[letterB release];
}
for (int i = 1; i <= 5; i++)
{
letterC = [[LetterTiles alloc] initWithFrame:CGRectMake(185, 104, 70, 70)];
[letterC setImage:[UIImage imageNamed:@"c.png"]];
letterC.tag = i;
[letterC setUserInteractionEnabled:YES];
[self.view addSubview:letterC];
[letterC release];
}
}
我有几个问题:
我应该为每个字母创建一个数组,还是为所有字母创建一个数组?
我应该使用什么样的数组? NSMutableArray/NSArray 还是 NSMutableDictionary/NSDictionary? -我一直在试图弄清楚如何将所有这些字母添加到上述所有数组类型中,然后根据它们的字母和标签检索它们,但我一生都无法弄清楚。有人愿意向我展示一个包含代码中的一两个字母的示例吗?
有没有比我在这里展示和询问更好的方法来做到这一点?
I'm creating draggable letter tiles for all the letters in the alphabet. I'm creating 5 of each letter in the case that a word requires more than one of the same letter.
I'm programmatically creating each UIImageView in my MainViewControllers ViewDidLoad: method.
- (void)ViewDidLoad:
{
for (int i = 1; i <= 5; i++)
{
letterA = [[LetterTiles alloc] initWithFrame:CGRectMake(39, 104, 70, 70)];
[letterA setImage:[UIImage imageNamed:@"a.png"]];
letterA.tag = i;
[letterA setUserInteractionEnabled:YES];
[self.view addSubview:letterA];
[letterA release];
for (int i = 1; i <= 5; i++)
{
letterB = [[LetterTiles alloc] initWithFrame:CGRectMake(112, 104, 70, 70)];
[letterB setImage:[UIImage imageNamed:@"b.png"]];
letterB.tag = i;
[letterB setUserInteractionEnabled:YES];
[self.view addSubview:letterB];
[letterB release];
}
for (int i = 1; i <= 5; i++)
{
letterC = [[LetterTiles alloc] initWithFrame:CGRectMake(185, 104, 70, 70)];
[letterC setImage:[UIImage imageNamed:@"c.png"]];
letterC.tag = i;
[letterC setUserInteractionEnabled:YES];
[self.view addSubview:letterC];
[letterC release];
}
}
I have a few questions:
Should I be creating an array for each letter, or creating a single array for all the letters?
What kind of array should I be using? NSMutableArray/NSArray or NSMutableDictionary/NSDictionary? -I have been trying to figure out how to add all these letters to all the above array types, then retrieve them based off their letter and tag, but cannot for the life of me figure it out. Would someone be willing to show me an example with one or two of the letters in code?
Is there a better way to do this than what I'm showing and asking here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题 1
对于所有字母使用一个 NSArray 更好,它减少了您必须处理的变量数量。拥有这样的东西:
只是可以在单个数据结构中轻松访问的大量代码,更不用说,如果您计划添加数字、标点符号等,则更容易更新。
问题 2
可变意味着它可以被更新,所以重要的问题是,你的数组/字典应该在运行时更新吗?如果答案是否定的,那么非可变数组可能会更好。另一方面,您必须自动用很多东西填充数组,因此拥有可变版本可能会让您拥有更清晰的代码(而不是使用
[[NSArray alloc] initWithObjects:[UIImage imageNamed :@“A”],...]
)。所以我会选择可变版本,只是为了更清晰的代码。
问题 3
这是个人选择,但我不太喜欢在静态类型语言中使用嵌套数据结构(在 python 中这样做是一种幸福,例如
myDictArray["key"][1])。我会使用可变字典(其中组合了键)来存储相关数据:
另一方面,我不使用图像,而是使用大方形 UILabels,以保存在内存中(以及心理理智,处理那么多数据)图像,呃),然后选择 iOS 使用的多种字体之一(这是完整列表)。
这样,生成所有内容,将需要两个嵌套的 for,一个迭代每个字母,然后每 5 次重复一个,然后创建标签并将其放入字典中(我会编写代码,但我不会一台苹果机)。
Question 1
Having a single NSArray for all the letters is better, it reduces the amount of variables you'll have to deal with. Having something like this:
Is just a lot of code for something that can be made readily accessible in a single data structure, not to mention, easier to update if you plan on adding numbers, puncutation, etc.
Question 2
Mutable means that it can be updated, so the important question, should your Array/Dictionary update on runtime? If the answer is no, a non-mutable array might be better. On the other hand, you'll have to automatically fill the array with a lot of stuff, so having a mutable version may allow you to have cleaner code (Instead of having a
[[NSArray alloc] initWithObjects:[UIImage imageNamed:@"A"], ...]
).So I would go with the mutable version, just for cleaner code.
Question 3
This is a personal choice, but I'm not a big fan of having nested data structures in statically typed languages (Doing this in python is a bliss, for instance
myDictArray["key"][1]
). I would use a Mutable Dictionary, where the keys are composed, to store the pertinent data:On the other hand, instead of using images, I would use big square UILabels, to save up in memory (and mental sanity, dealing with that many images, ugh), and pick one of the many fonts that iOS uses (Here's a complete list).
With this, generating everything, would require two nested fors, one to iterate through each letter, and then one for each 5 repetitions, and then create the label and throw it inside the dictionary (I would write code, but I'm not on a mac).