2012-06-02 40 views
0

可能重複:
access to array object節目從陣列圖像的UIImageView

我有包含圖像的數組,並在我的廈門國際銀行文件的ImageView的。 現在, 我想在我的imageview中查看我的數組包含的第一個對象(圖像)。 我試圖做幾個小時,但我沒有成功。我該怎麼做??

公噸陣列減速:

photoArray = [[NSMutableArray alloc] init]; 
    PhotoItem *photo1 = [[PhotoItem alloc] initWithPhoto:[UIImage imageNamed:@"1.jpg"] name:@"roy rest" photographer:@"roy"]; 
    PhotoItem *photo2 = [[PhotoItem alloc] initWithPhoto:[UIImage imageNamed:@"2.jpg"] name:@"roy's hand" photographer:@"roy"]; 
    PhotoItem *photo3 = [[PhotoItem alloc] initWithPhoto:[UIImage imageNamed:@"3.jpg"] name:@"sapir first" photographer:@"sapir"]; 
    PhotoItem *photo4 = [[PhotoItem alloc] initWithPhoto:[UIImage imageNamed:@"4.jpg"] name:@"sapir second" photographer:@"sapir"]; 
    [photoArray addObject:photo1]; 
    [photoArray addObject:photo2]; 
    [photoArray addObject:photo3]; 
    [photoArray addObject:photo4]; 

,我試圖一起工作的代碼:

imgView = [[UIImageView alloc] initWithImage:[photoArray objectAtIndex:0]]; 

謝謝!

+0

我看到你問了一個非常類似的問題,我回答。 http://stackoverflow.com/questions/10864893/access-to-array-object/10868444#10868444你也不接受那一個。在Objective-C/iOS上購買一本書並閱讀它們。你不需要提出這樣的問題,它會大大提高你對編碼的理解! – Ben

回答

0

你的照片數組不包含圖像,它包含自定義的PhotoItem對象。你需要從該對象中獲取UIImage。我假設你有一套可能在PhotoItem上有名的照片。你需要調用這個。

imgView = [[UIImageView alloc] initWithImage:[[photoArray objectAtIndex:0] photo]]; 

// or 

imgView = [[UIImageView alloc] initWithImage:[photoArray objectAtIndex:0].photo]; 

UPDATE

你必須有屬性訪問有關對象的信息。您需要重建您的PhotoItem類以便爲您想要訪問的內容提供屬性,如照片,名稱和攝影師。實例變量無法以您嘗試的方式訪問。

// PhotoItem.m 

@interface PhotoItem : NSObject 
{ 
    // Instance Variables aren't accessible outside of the class. 
} 

@property (nonatomic, strong) UIImage *photo; 
@property (nonatomic, strong) NSString *name; 
@property (nonatomic, strong) NSString *photographer; 

- (id) initWithPhoto:(UIImage)image name:(NSString*)stringA photographer:(NSString*)stringB; 

而且你也需要重寫init。

// PhotoItem.h 

@implementation PhotoItem 

@synthesize photo, name, photographer; 

- (id) initWithPhoto:(UIImage)image name:(NSString*)stringA photographer:(NSString*)stringB 
{ 
    self = [super init]; 
    if (self) { 
     photo = image; 
     name = stringA; 
     photographer = stringB; 
    } 
} 

然後訪問任何其

PhotoItem *photo1 = [[PhotoItem alloc] initWithPhoto:[UIImage imageNamed:@"1.jpg"] name:@"roy rest" photographer:@"roy"]; 

photo1.photo // returns UIImage 
photo1.name // returns NSString 
photo1.photographer // returns NSString 

因此,如果他們是在一個數組對象這將是

[photoArray objectAtIndex:0] // returns your PhotoItem Object 
[[photoArray objectAtIndex:0] photo] // returns that PhotoItem Object then gets the photo out of it returning a UIImage in total. 
+0

我嘗試過,但它仍然不起作用 – APRULE

+0

然後你需要顯示更多的代碼。張貼PhotoItem的接口。 –

+0

這裏是PhotoItem。h:'code'@interface PhotoItem:NSObject { UIImage * imageView; NSString * photoNameLabel; NSString * photographerNameLabel; UIButton * viewPhoto;} // - (id)makePhotoItemWIthPhoto:(UIImage *)圖片名稱:(NSString *)photoName攝影師:(NSString *)photographerName; (NSString *)photoName:(NSString *)photographerName;(NSString *) – APRULE

1

回答你的問題是嘗試很簡單,做它如果你對所有這些都是新手,那麼就採取措

array: photoarray 
imageView in xib : imgView 
imgView =[UIImageView alloc]init]; 
imgView.image=[UIImage imageNamed:[photoarray objectatindex:0]]; 

這將用於您的問題的目的。