gpt4 book ai didi

iphone - 我可以重新编码循环 UIImageView 以获得更短的代码吗?

转载 作者:行者123 更新时间:2023-11-29 05:05:01 25 4
gpt4 key购买 nike

我可以让这段代码比这个短吗?

- (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)
{
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);
}
}
}

如果可以再短一点吗?请帮助我该怎么做?

最佳答案

是的,可以更短,看看这个

- (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);
}
}

关于iphone - 我可以重新编码循环 UIImageView 以获得更短的代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5752210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com