【问题标题】:iOS - Storing groups of UILabels into a NSMutableArrayiOS - 将 UILabel 组存储到 NSMutableArray
【发布时间】:2011-07-18 17:04:26
【问题描述】:

我正在为每个循环动态创建UILabels。运行的每个循环都会创建 1-4 个 UILabel。

我想要的是我将这些 UILabel 放入我的 NSMutableArray 中,以便以后能够轻松检索数据。

我最初的想法是将这些 UILabel 放入 NSDictionary 并使用 [dictGroupLabels setValue:uiLabel1 forKey:@"uiLabel1"] 然后使用 [dictGroupLabels setValue:uiLabel2 forKey:@"uiLabel2"] 等等。然后为每个循环将这个字典放入我的 NSMutableArray 中。稍后我可以访问像 UILabel *label = [[myArray objectAtIndex:0] valueForKey:@"uiLabel1"] BUT 这样的值,不幸的是,由于 UILabel 不符合 NSCopying 协议,所以它不起作用。

那么考虑到这一点,您将如何解决这个问题?

【问题讨论】:

  • 良好实践表明,如果处理静态数量的标签,则每个 UILabel 都有一个 (n IBOutlet) 变量。否则,您应该能够将引用放入字典或数组中,然后再将它们拉出。但是,它们应该是对原始标签的引用:而不是副本。如果你想要副本,要么继承 UILabel 并实现类似 -(UILabel)copy 的方法,要么创建一个具有所有相同属性的新 UILabel。

标签: objective-c indexing nsmutablearray uilabel nsdictionary


【解决方案1】:

此问题提供了有关您要完成的工作的更多信息。既然您知道在每种情况下尝试创建的可能标签集,我强烈建议您使用 mutable dictionaries 而不是数组。

为了说明,给定以下假设的类定义:

@interface MyClass: NSObject { 
    NSMutableDictionary * _labelDict; 
} 

@property (nonatomic, retain) NSMutableDictionary * labelDict; 

- ( void )methodA; 
- ( void )methodB; 
- (NSMutableDictionary *) labelsForRunLoop: (NSUInteger) loopIdx;
@end

您将有以下假设的类实现:

@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

这基本上是伪代码,不会成功编译。但是,它将作为一个很好的起点。您可能希望将每个标签字典存储在某个有意义的键下,而不仅仅是使用循环的索引。希望这会有所帮助。

【讨论】:

  • 谢谢!这是一种聪明的做法。两个问题,你为什么要在返回字典的方法中调用retain(在调用setObject: 时,labelDict 不是自动保留该字典吗?虽然不是问题,但你没有在@987654326 中使用loopIdx @方法。
  • @Peter Warbo - 实际上,只要您像我一样将对象添加到另一个字典中,在返回之前调用保留是多余的。您应该改用自动释放。此外,我添加的 labelsForRunLoop 方法仅作为示例。我不知道您使用什么逻辑来确定您需要创建哪些 W、X、Y、Z 标签,所以我创建了一个简单的示例,虽然我没有实际使用它,但它采用了运行循环索引。当然,你得到了一般的要点,所以你可以使用伪代码来实现你需要的功能。
【解决方案2】:

它们不需要遵守 NSCopying 就可以添加到数组中。听起来你只需要这样做:

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

【讨论】:

  • 我需要一些东西来识别我正在检索的标签。它可以是 wLabel、xLabel、yLabel 或 zLabel。有时只有一个 wLabel,有时是全部 4 个。这是使用数组数组的唯一方法吗?钥匙会很方便...
  • 不,您可以使用字典数组。但是,如果您希望能够在循环中向它们添加多个内容,它们必须是 NSMutableDictionary 的实例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-16
  • 1970-01-01
  • 1970-01-01
  • 2017-03-31
相关资源
最近更新 更多