0

我怎样才能在这里改进我​​的代码,它有一个图像数组来拥有/分配一个特定的整数、值或数字。每当它显示时,我都可以以编程方式在其上画一个圆圈。有人可以建议一种方法吗?

- (void)awakeFromNib
{    
    if (self) {

        self.images = [NSMutableArray arrayWithObjects:@"111.jpg",
                       @"112.jpg",
                       @"113.jpg",
                       @"114.jpg",
                       @"115.jpg",
                       @"116.jpg",
                       @"117.jpg",
                       @"118.png",
                       @"119.jpg",
                       @"120.jpg",
                       nil];

    }
}
4

1 回答 1

1

Create an array of NSDictionaries (instead of strings) and each dictionary can contain a key for the image name plus whatever other keys you want to add. Or even better, create an array of your own custom object (subclass NSObject) and then you can access the properties of each entry in the array without having to do a dictionary lookup.

Here is some sample code for the NSDictionary method:

- (id)init
{
    ...
    images = [[NSMutableArray alloc] init];
    ...
}

- (void)dealloc
{
    ...
    [images release];
    [super dealloc];
}

- (void)addImagesToArrayWithImageName:(NSString *)imageName andTag:(int)tag
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:imageName forKey:@"imageName"];
    [dict setObject:[NSNUmber numberWithInt:tag] forKey:@"tag"];
    [images addObject:dict];
}

- (void)awakeFromNib
{
    [self addImagesToArrayWithImageName:@"111.jpg" andTag:0];
    [self addImagesToArrayWithImageName:@"112.jpg" andTag:1];
    ...
}
于 2012-05-08T07:44:00.123 回答