iOS - 将UILabel组存储到NSMutableArray中 [英] iOS - Storing groups of UILabels into a NSMutableArray
问题描述
我正在为每个循环动态创建 UILabel
。每个运行的循环都会创建1-4个UILabel。
I'm creating UILabel
s dynamically in a for each loop. Every loop that is run creates 1-4 UILabels.
我想要的是将这些UILabel放入我的NSMutableArray中,以后能够轻松检索数据。
What I want is that I put these UILabels into my NSMutableArray and being able later to easy retrieve the data.
我最初的想法是将这些UILabel放入 NSDictionary
并使用 [dictGroupLabels setValue:uiLabel1 forKey:@uiLabel1]
然后 [dictGroupLabels setValue:uiLabel2 forKey:@uiLabel2]
依此类推。然后将这个字典放入我的NSMutableArray中,用于每个循环。稍后我可以访问像 UILabel * label = [[myArray objectAtIndex:0] valueForKey:@uiLabel1]
但这样的值,但不幸的是UILabels不符合NSCopying协议。
My original thought was to put these UILabels into a NSDictionary
and use [dictGroupLabels setValue:uiLabel1 forKey:@"uiLabel1"]
and then [dictGroupLabels setValue:uiLabel2 forKey:@"uiLabel2"]
and so on. And then put this dictionary into my NSMutableArray for each loop. Later on I could access the values like UILabel *label = [[myArray objectAtIndex:0] valueForKey:@"uiLabel1"]
BUT that unfortunately doesn't work since UILabels don't conform to the NSCopying protocol.
所以考虑到这一点你会如何解决这个问题?
So with this in mind how would you solve this?
推荐答案
这个问题提供了有关您要完成的内容的更多信息。既然你知道一个事实,你想要在每种情况下创建的标签集,我强烈建议使用 mutable dictionaries 而不是数组。
this question provided more information on what you are trying to accomplish. Since you know for a fact, the possible set of labels you are trying to create in each case, I would highly recommend using mutable dictionaries instead of arrays.
为了说明,给出了遵循假设的类定义:
To illustrate, given the following hypothetical class definition:
@interface MyClass: NSObject {
NSMutableDictionary * _labelDict;
}
@property (nonatomic, retain) NSMutableDictionary * labelDict;
- ( void )methodA;
- ( void )methodB;
- (NSMutableDictionary *) labelsForRunLoop: (NSUInteger) loopIdx;
@end
您将拥有以下假设的类实现:
You would have the following, hypothetical, class implementation:
@implementation MyClass
@synthesize labelDict = _labelDict;
- ( id ) init {
if( ( self = [ super init ] ) ) {
[self setLabelDict: [NSMutableDictionary dictionaryWithCapacity: 8]];
}
}
- ( void ) dealloc {
[ self.labelDict release ];
[ super dealloc ];
}
- ( void ) methodA {
for(NSUInteger i = 0; i < some index; i++) {
[self.labelDict setObject: [self labelsForRunLoop: i] forKey: [NSString stringWithFormat: @"%d", i]];
}
}
- ( void ) methodB {
// Locate the label you need to work with. Example based on this crude pseudo code
NSMutableDictionary * subDict = (NSMutableDictionary *) [self.labelDict objectForKey: @"0"];
UILabel * theLabel = (UILabel * ) [subDict objectForKey: @"UILabel.Z"];
theLabel.text = @"Label 1";
}
- (NSMutableDictionary *) labelsForRunLoop: (NSUInteger) loopIdx {
NSMutableDictionary * dictionary = [NSMutableDictionary dictionaryWithCapacity: 4] ;
[dictionary setObject: create-w-label forKey: @"UILabel.W"];
[dictionary setObject: create-x-label forKey: @"UILabel.X"];
[dictionary setObject: create-y-label forKey: @"UILabel.Y"];
[dictionary setObject: create-z-label forKey: @"UILabel.Z"];
return [dictionary retain];
}
@end
这基本上是伪代码将无法成功编译。然而,它将是一个很好的起点。您可能希望将每个标签字典存储在一些有意义的键下,而不是仅使用循环索引。希望这会有所帮助。
This is basically pseudo code and will not successfully compile. However it will serve as a good starting point. You probably want to store each label dictionary under some key that makes sense, instead of just using the loop's index. Hope this helps.
这篇关于iOS - 将UILabel组存储到NSMutableArray中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!