iOS - Storing groups of UILabels into a NSMutableA

2019-09-18 15:17发布

I'm creating UILabels dynamically in a for each loop. Every loop that is run creates 1-4 UILabels.

What I want is that I put these UILabels into my NSMutableArray and being able later to easy retrieve the data.

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?

2条回答
时光不老,我们不散
2楼-- · 2019-09-18 15:22

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.

查看更多
劳资没心,怎么记你
3楼-- · 2019-09-18 15:39

They don’t need to adhere to NSCopying to be added to an array. It sounds like you just need to do something like this:

NSMutableArray *mainArray = [NSMutableArray array];

for(int i = 0; i < 5; i++)
{
    NSMutableArray *subArray = [[NSMutableArray alloc] initWithCapacity:5];

    for(int j = 0; j < 4; j++)
    {
        UILabel *label = [[UILabel alloc] init];
        // etc.
        [subArray addObject:label];
        [label release];
    }
    [mainArray addObject:subArray];
    [subArray release];
}

// then, to get one of the labels:

UILabel *someSpecificLabel = [[mainArray objectAtIndex:2] objectAtIndex:1];
查看更多
登录 后发表回答