开发者

How do I init a NSArray with a set of images that reocur?

EDIT: I think i asked something really wrong over here, and i am sorry. What i am doing, is a mock of a slot machine, but i don't think that it will be wise(just don't want to insult myself) of me to put 250(50*5) images in the UIPICKERVIEW, There is no reason doing that, My idea now is to use on开发者_开发技巧ly the 6 images for each column in deferent order and then when i spin just go again and again over the 30(6*5) images and spin them, that will give the same effect while using less resources, right? I can't see what i was thinking, and thank you all for the answers

Implementing a complex UIPickerView (5 components) in iOS, I am stuck on this problem...

I have 5 NSArrays (for each component in the picker...not mutable) and I have 6 images.

I need to init every array with the set of images, that will populate the array for 50 images, set after set (I'm guessing some kind of for loop that will run 8 times and populate the array with 2 more images at the end?)

In every array the images need to be in a different order, but again, for the same array the set has to be the same all the way.

When I say a set I mean that I need to put the images in the same order again and again, not that they are in another array or anything like that....

Can any one help with this?

10x, Erez


Not sure I understood the question, but, if I did, you may do:

NSArray *set = [NSArray arrayWithObjects:[UIImage imageNamed:@"img1.png"],/* all the images */ , nil];
NSMutableArray *tmp = [[NSMutableArray alloc] init];


for (int i = 0; i<50; i++) {
    [tmp addObject:[set objectAtIndex:(i%6)]];
}

then you may use tmp as common NSArray, or do something like

NSArray *result = [NSArray arrayWithArray:tmp];

ps. don't forget to release tmp when you finished.


UIImage        *image;
int             randomIndex;

NSCountedSet   *set    = [[NSCountedSet alloc] initWithCapacity:50];
NSMutableArray *arrays = [[NSMutableArray alloc] initWithCapacity:5];

for ( int i = 0; i < 5; i++ ) {
    while ( [set count] < 50 ) {
        randomIndex = arc4random() % 6;
        image = [images objectAtIndex:randomIndex];

        switch ( [set count] ) {
            case 48:
            case 49:
                if ( [set countForObject:image] < 9 ) {
                    [set addObject:image];
                }
                break;

            default:
                if ( [set countForObject:image] < 8 ) {
                    [set addObject:image];
                }
        }
    }

    [arrays addObject:[set allObjects]];
    [set removeAllObjects];
}

[set release];

The idea would be to use a counted set and get the count of elements added to it. This way we can keep track of objects being added.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜