2016-07-18 8 views
2

私はIOSで新しく、2D配列をどのように埋め込むのかはわかりませんcollectionView2D NSMutableArray in UICollectionView

**ラベルを使用して、コレクションビューで次のように表示する方法を教えてください。

myArray[0]->1,2,3,4,5 
myArray[1]->6,7,8,9 
myArray[2]->10,11,12,15 


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 

// What to call here as this is 2D? 

return self.myArray.count; 

} 


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
NSString *simpleId= @"CollectionViewCell"; 
TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleId forIndexPath:indexPath]; 

//What to write here ? Cell contain 1 Label 
} 
+0

私は推測します: 'collectionView:numberOfItemsInSection:' 'return [[self.myArray objectAtIndex:section] count];'、 'numberOfSection':' return [self.myArray count] '、コレクションビュー:cellForRowAtIndexPath: ':' NSString * item = [[self.myArray objectAtIndexPath:[indexPath section] objectAtIndex:[indexPath row]]; '? – Larme

+0

[[self.myArray objectAtIndex:section] count]; これは、 "制御が非void関数に戻ります"というエラーを返します。 –

+0

@SyedMunimRazaようこそstackoverflowへと私の下の答えを確認してください..これはあなたの問題を解決するのに役立つことを願っています。 –

答えて

1

(@Larmeが示唆したように)、このように行うことを試みる - あなたの2次元アレイMyArrayというで

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ 

    NSString *simpleId= @"CollectionViewCell"; 
    TableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpleId forIndexPath:indexPath]; 

    cell.lbl.text= [NSString stringWithFormat:@"%@",[[self.myArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]]; 

    return cell; 

} 

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{ 

    return self.myArray.count; 
} 


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{ 

    return [[self.myArray objectAtIndex:section] count] 
} 

[0]は、最初のセクションは、5値、MyArrayというで構成である[1]であります2番目のセクションは4つの値で構成され、myArray [2]は5番目のセクションで構成されます。 2DアレイはnumberOfSectionnumberOfItemsInSectionの方法で簡単に管理できます。これはあなたを助けることを願っています..

+1

ありがとうAlarm LarmeとDev.RK –

+0

Dev.RKマイナーな問題があります。 ' - (NSInteger)collectionView:(UICollectionView *)コレクションビューnumberOfItemsInSection:(NSInteger)セクション{ 戻り値[[self.myArray objectAtIndex:section] count]; } ' **このセクションでは、増分していないので常に0のままです** –

+0

[[self.myArray objectAtIndex:section] count]と[self.myArray objectAtIndex:section]は最初にサイズを出力し、 2行目は配列を出力します。 –