开发者

Can I re-coding loop UIImageView to get shorter code?

Can I make this code shorter than this ?

- (void) setupFeature
{   NSArray *numbers = [NSArray arrayWithObjects: @"01", @"02", @"03",@"04",@"05",@"06", nil];
    position = CGRectMake(7, position.origin.y+20, 72, 72);

    int j=0;
    NSString *pic;
    UIImageView *a_pic;
    NSMutableArray *C_Pic = [[[NSMutableArray alloc] init] autorelease];
    for (int i=0; i<[numbers count]; i++) 
    { 
        UIImageView *picture = [[UIImageView alloc] init];
        [C_Pic addObject:picture];
        [picture release];
        a_pic = [C_Pic objectAtIndex:i];
        pic = [NSString stringWithFormat:@"iconD%@",[numbers objectAtIndex:i]];
        a_pic.image = [UIImage imageWithContentsOfFile:[开发者_JAVA技巧[NSBundle mainBundle] pathForResource:pic ofType:@"png"]];
        if(j<4)
        {   
            a_pic.frame = position;
            [scrollView addSubview:a_pic];
            position = CGRectMake(position.origin.x+77, position.origin.y, 72, 72);
            j++;
        }
        else
        {   
            j=0;
            position = CGRectMake(7, position.origin.y+77, 72, 72);
            NSLog(@"Pic%i position %@",i, NSStringFromCGRect(self.position));
            a_pic.frame = position;
            [scrollView addSubview:a_pic];
            position = CGRectMake(position.origin.x+77, position.origin.y, 72, 72);
        }
    }
}

If it can be shorter ?? please , help me how to do that ?


Yes it can be shorter see this

- (void) setupFeature
{   
NSArray *numbers = [NSArray arrayWithObjects: @"01", @"02", @"03",@"04",@"05",@"06", nil];
    position = CGRectMake(7, position.origin.y+20, 72, 72);

    int j=0;
    NSString *pic;
    UIImageView *a_pic;
    NSMutableArray *C_Pic = [[[NSMutableArray alloc] init] autorelease];
    for (int i=0; i<[numbers count]; i++) 
    { 
        UIImageView *picture = [[UIImageView alloc] init];
        [C_Pic addObject:picture];
        [picture release];
        a_pic = [C_Pic objectAtIndex:i];
        pic = [NSString stringWithFormat:@"iconD%@",[numbers objectAtIndex:i]];
        a_pic.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]      pathForResource:pic ofType:@"png"]];
        if(j<4)
        {   

            j++;
        }
        else
        {   
            j=0;
            position = CGRectMake(7, position.origin.y+77, 72, 72);
            NSLog(@"Pic%i position %@",i, NSStringFromCGRect(self.position));

        }
    //No need to repeat this code
             a_pic.frame = position;
            [scrollView addSubview:a_pic];
            position = CGRectMake(position.origin.x+77, position.origin.y, 72, 72);
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜