Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I've spend the last several hours working on a loop designed to take items in an array and arrange them as a so to speak "gird" of UIButtons. As far as I've been able to see through testing this code works perfectly.

However, I am new to creating loops and I was wondering if anyone could give me any pointers on how I could make this code cleaner/more efficient for future coding purposes.

I know my question is very similar to Optimize sorting of Array according to Distance (CLLocation), but I'm really hoping to get some tips on a "per case" basis. Thank you for your time.

- (void)myMethod
{
    NSArray *array = [[NSArray alloc] initWithObjects:@"Item #1", @"Item #2", @"Item #3", @"Item #4", @"Item #5", @"Item #6", @"Item #7", @"Item #8",  nil];

    BOOL leftOkay = NO;
    BOOL centerOkay = NO;
    BOOL rightOkay = NO;
    int left = 0;
    int center = 1;
    int right = 2;
    int yOrigin = 0;
    int spaceBetweenItems = 5;
    UIButton *leftButton;
    UIButton *centerButton;
    UIButton *rightButton;

    for (int i = 0; i < [array count]; i++)
    {
        if (left < [array count]) {
            leftButton = [[UIButton alloc] initWithFrame:CGRectMake(spaceBetweenItems, yOrigin + spaceBetweenItems, 100, 150)];
            [leftButton setTitle:[array objectAtIndex:left] forState:UIControlStateNormal];
            [leftButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
            [leftButton setBackgroundColor:[UIColor cyanColor]];
            [myScrollView addSubview:leftButton];
            leftOkay = YES;
        }
        if (center < [array count]) {
            centerButton = [[UIButton alloc] initWithFrame:CGRectMake(110, yOrigin + spaceBetweenItems, 100, 150)];
            [centerButton setTitle:[array objectAtIndex:center] forState:UIControlStateNormal];
            [centerButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
            [centerButton setBackgroundColor:[UIColor yellowColor]];
            [myScrollView addSubview:centerButton];
            centerOkay = YES;
        }
        if (right < [array count]) {
            rightButton = [[UIButton alloc] initWithFrame:CGRectMake(215, yOrigin + spaceBetweenItems, 100, 150)];
            [rightButton setTitle:[array objectAtIndex:right] forState:UIControlStateNormal];
            [rightButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
            [rightButton setBackgroundColor:[UIColor purpleColor]];
            [myScrollView addSubview:rightButton];
            rightOkay = YES;
        }
        if (leftOkay == YES && centerOkay == YES && rightOkay == YES) {
            yOrigin = yOrigin + leftButton.frame.size.height + (spaceBetweenItems * 2);
            leftOkay = NO;
            centerOkay = NO;
            rightOkay = NO;
        }

        left    = left    + 3;
        center  = center  + 3;
        right   = right   + 3;
    }
    myScrollView.contentSize = CGSizeMake(myScrollView.frame.size.width, yOrigin + leftButton.frame.size.height + (spaceBetweenItems * 2));
}
share|improve this question

1 Answer

up vote 2 down vote accepted

you can write this code dramatically shorter like

self.view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];

NSArray *array = [[NSArray alloc] initWithObjects:@"Item #1", @"Item #2", @"Item #3", @"Item #4", @"Item #5", @"Item #6", @"Item #7", @"Item #8",  nil];

NSUInteger buttonWidth = 100;
NSUInteger buttonHight = 100;
NSUInteger spacer = 5;
NSUInteger leftOffset = 5;
NSUInteger topOffset = 5;
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    int row = idx / 3;
    int column = idx %3;

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake((buttonWidth + spacer)*column+leftOffset, (buttonHight+spacer)*row +topOffset , buttonWidth    , buttonHight);
    [button setTitle:(NSString *)obj forState:UIControlStateNormal];
    [self.view addSubview:button];
}];

Note: ARC enabled

share|improve this answer
edited to have central defined variables – ikinci viking Aug 12 '12 at 15:18
Wow that is a lot better, thank you for the help!!! – 0x7fffffff Aug 12 '12 at 15:29
If you'd like to have some explanations, just go ahead and ask :) – ikinci viking Aug 12 '12 at 16:02
I appreciate it, my implementation of this is fairly simple, the array is actually a list of image file names 0 to x in NSDocumentsDirectory which .jpg is later appended to. So then I'm linking all the buttons to a void with a UIButton argument so I can pass the currently selected images frame property in order to animate the button/image growing to full screen when it is selected. Would you say this is a good way of doing this? – 0x7fffffff Aug 12 '12 at 16:19
sounds reasonable to me. – ikinci viking Aug 12 '12 at 16:27
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.