2014-10-07 48 views
-2

我想訪問其中一個數組並顯示所選特定數組的所有內容。iOS僅在一個二維數組陣列中訪問

這是代碼:

老錯誤的代碼片段

NSArray *prova = [[NSArray alloc] initWithObjects:@"http://i.imgur.com/3IBezow.jpg", nil]; 

_BIGArray = [[NSArray alloc] initWithObjects:prova, prova1, nil]; 


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 

    // Configure the cell 
    cell.gridImage.image = [UIImage imageNamed:@"loading.png"]; 

    url = [[_BIGArray objectAtIndex:_idArray]objectAtIndex:_idArray]; 

    cell.gridImage.associatedObject = url; 

    [self.operationManager GET:url 
        parameters:nil 
         success:^(AFHTTPRequestOperation *operation, id responseObject) { 
          if ([cell.gridImage.associatedObject isEqualToString:url]) 
          { 
           cell.gridImage.image = responseObject; 
          } 
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
          NSLog(@"Failed with error %@.", error); 
         }]; 
    return cell; 
} 

更新的代碼

collectionviewcontroller.h

@property (nonatomic, strong) NSArray *BIGArray; 
@property (nonatomic, retain) AFHTTPRequestOperationManager *operationManager; 
@property (nonatomic, assign) NSInteger idArray; 

collectionviewcontroller.m

NSArray *prova = [[NSArray alloc] initWithObjects: @"http://i.imgur.com/3IBezow.jpg", nil]; 

NSArray *prova1 = [[NSArray alloc] initWithObjects: @"http://i.imgur.com/yE4egi4.jpg", nil]; 

_BIGArray = @[prova, prova1]; 

- (AFHTTPRequestOperationManager *)operationManager 
{ 
    if (!_operationManager) 
    { 
     _operationManager = [[AFHTTPRequestOperationManager alloc] init]; 
     _operationManager.responseSerializer = [AFImageResponseSerializer serializer]; 
    }; 

    return _operationManager; 
} 

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath]; 

    // Configure the cell 
    cell.gridImage.image = [UIImage imageNamed:@"loading.png"]; 

    NSString *url = [_BIGArray objectAtIndex:_idArray]; 

    cell.gridImage.associatedObject = url; 

    [self.operationManager GET:url 
        parameters:nil 
         success:^(AFHTTPRequestOperation *operation, id responseObject) { 
          if ([cell.gridImage.associatedObject isEqualToString:url]) 
          { 
           cell.gridImage.image = responseObject; 
          } 
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
          NSLog(@"Failed with error %@.", error); 
         }]; 
    return cell; 
} 

如果我寫這行代碼:

NSString *url = [_BIGArray objectAtIndex:_idArray]; 

我獲得以下錯誤:

2014-10-07 05:09:29.444 prova_prj[497:70b] ID: 0 
2014-10-07 05:09:29.454 prova_prj [497:70b] -[__NSArrayI length]: unrecognized selector sent to instance 0x7b1a9400 
2014-10-07 05:09:29.464 prova_prj [497:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI length]: unrecognized selector sent to instance 0x7b1a9400' 

而且我的目標是選擇陣列(使用索引查看我從前面的視圖中獲得)並顯示所有圖像。 Objective-C有可能嗎?在Java和C#中,我沒有遇到任何重大問題。


SOLUTION

NSMutableArray *array = [[NSMutableArray alloc]initWithCapacity:2]; 
    [array insertObject:[NSMutableArray arrayWithObjects:@"http://i.imgur.com/3IBezow.jpg", @"http://i.imgur.com/HyDJrDX.jpg", nil] atIndex: 0]; 
[array insertObject:[NSMutableArray arrayWithObjects:@"http://i.imgur.com/yE4egi4.jpg", @"http://i.imgur.com/twc1Ml5.jpg", @"http://i.imgur.com/66V4dhe.jpg", nil] atIndex:1]; 

然後,我已經改變了_BIGArray串碼:

_BIGArray = [array objectAtIndex:_idArray]; 

然後我改變的代碼URL字符串:

NSString *url = self.BIGArray[indexPath.row]; 


謝謝大家。

+0

你可以使用一個for循環從1到數組的末尾取所有元素......但是pheraphs我錯過了你的問題。 – 2014-10-07 10:14:57

+0

...無論如何,哪裏是二維數組?因爲在你的代碼片段中你沒有任何,只是一個簡單的一維數組。 – holex 2014-10-07 10:49:22

+0

是的,你是正確的,只有一個陣列,但因爲我很着急,我認爲他採取了兩個去,我更新了陣列 – Matteo 2014-10-07 11:49:05

回答

1

您可以訪問數組作爲頂部數組的第一個元素。像

NSArray *urls = (NSArray *)[_BIGArray objectAtIndex:_idArray];

+0

寫道:坦率地說,如果這不是明顯的OP那麼他不應該試圖代碼在Objective-C。他需要首先學習基本的編程概念,Objective-C不是初學者的語言。 – 2014-10-07 12:25:05

+0

我沒有新的節目,但我是新對象的C,我在Java和C#編程以較少的困難Objective-Ç – Matteo 2014-10-07 12:34:24

0

_BIGArray中有隻有一個對象,它是PROVA。我不明白你如何檢索[1] [1]甚至[1]。 你可以檢索_BIGArray[0][x](其中x的範圍是0到prova.count-1)或_BIGArray[0](它會給你數組prova返回)。

編輯:

NSArray *first = _BIGArray[0]會給你陣列PROVA NSArray *second = _BIGArray[1]會給你陣列prova1。

這應該做你所需要的。

+0

事實上,他的第二個版本中有兩個數組。 – 2014-10-07 12:27:18

+0

沒有看到新的代碼。看我的編輯。 – 2014-10-07 12:37:22