我正在尝试用UIImageViews加载NSMutableArray。一切都很顺利。
不幸的是,当对象在可变数组中时,我不知道如何使用它们。
下面是一些代码:
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
NSMutableArray *array = [NSMutableArray new];
[array loadWithObject:(UIImageView *)imageView];
[imageView release];这就是我所做的事情。这是我想要做的:
[array objectAtIndex:5].center = GCRectMake(0, 0);但这并不管用。我该怎么做呢??
发布于 2010-04-10 13:38:51
好的,我会解释你遇到的问题。做你想做的事情的方法如下:
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
NSArray *array = [NSArray arrayWithObject:imageView];
[imageView release];
[[array objectAtIndex:0] setCenter:CGPointMake(0.0,0.0)];首先,没有-[NSMutableArray loadWithObject:]方法。同样,对于您的示例,您甚至不需要可变数组。可变对象有自己的位置,但我通常会在有意义时尝试使用不可变对象;因此,我使用了NSArray。
接下来,在将对象添加到数组中时,您永远不需要对对象进行类型转换。有几个原因导致你的例子不起作用:
UIImageView的实例吗?CG,而不是GC。此外,-[UIImageView setCenter:]接受CGPoint,而不是CGRect。所以你想要的函数是CGPointMake.祝你好运!如果这有助于澄清一些事情,请让我知道。
发布于 2010-04-10 13:39:05
我认为你应该推荐NSMutableArray。
然而,我只是给出了NSMutableArray的概述。
NSMutableArray *anArray=[[NSMutableArray alloc] init];
[anArray addObject:@"Sagar"];
[anArray addObject:@"pureman"];
[anArray addObject:@"Samir"];
中存储imageViews
NSMutableArray *anArray=[[NSMutableArray alloc] init];
UIImageView *imgV1=[[UIImageView alloc] initWithFrame:CGRectMake(10,50,60,70)];
UIImageView *imgV2=[[UIImageView alloc] initWithFrame:CGRectMake(10,110,60,70)];
UIImageView *imgV3=[[UIImageView alloc] initWithFrame:CGRectMake(10,170,60,70)];
UIImageView *imgV4=[[UIImageView alloc] initWithFrame:CGRectMake(10,210,60,70)];
[anArray addObject:imgV1];
[anArray addObject:imgV2];
[anArray addObject:imgV3];
[anArray addObject:imgV4];
[imgV1 release];
[imgV2 release];
[imgV3 release];
[imgV4 release];
上面的代码会将图片添加到NSMutableArray
UIImageView *x=[anArray objectAtIndex:0];
https://stackoverflow.com/questions/2612328
复制相似问题