2013-09-25 18 views
15

Chcę wyświetlić UICollectionView, który zawiera dokładnie 2 wiersze i 100 komórek w każdym wierszu.Określ numer wiersza i kolumny dla UICollectionView

// 1 
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section { 

    return 100; 
} 
// 2 
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView { 
    return 2; 
} 
// 3 
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; 

     cell.lbl.text = @"Data"; 

    return cell; 
} 

jestem coraz to:

enter image description here

Jak mogę określić numer wiersza dla UICollectionView? Chcę utworzyć tabelę 2x100.

Odpowiedz

11

Spróbuj tego:

// 1 
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section { 

    return 2; 
} 
// 2 
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView { 
    return 100; 
} 
// 3 
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath { 
    MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; 

     cell.lbl.text = @"Data"; 

    return cell; 
} 
+0

Dziękuję bardzo ... – onivi

+42

wydaje się nieprawidłowy – aelam

+0

Lepiej jest używać niestandardowego układu: http://stackoverflow.com/a/35362818/833885 – brianLikeApple

2

liczba sekcji dyktuje jak wiele komórek będzie w każdym rzędzie, przy założeniu, że wielkość komórek na to pozwala.

Sekcja zawsze rozpoczyna nowy wiersz.

13

Dla 3 komórek w rzędzie .. Dodaj ten kod.

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    return CGSizeMake(collectionView.frame.size.width/3.2 , 100); 
} 
+2

Dla tej metody wymagany jest protokół 'UICollectionViewDelegateFlowLayout'. –

+1

Tak, potrzebujemy. Spójrz na: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewDelegateFlowLayout_protocol/ –

+0

Powinien to być 3 komórki w rzędzie lub liczba kolumn. Dobrze? – malajisi

9

Swift: 3 rzędy na kolumnę (przy użyciu @SakhshiSingla Answer)

override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
    return 1 
} 

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return 3 
} 

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize 
    { 
     return CGSize(width: collectionView.frame.size.width/3.2, height: 100) 
    } 
9

Bardziej ogólne podejście do uzyskania n kolumn widzenia odbioru, który jest kompatybilny z dowolnego orientacja

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 

    // flow layout have all the important info like spacing, inset of collection view cell, fetch it to find out the attributes specified in xib file 
    guard let flowLayout = collectionViewLayout as? UICollectionViewFlowLayout else { 
     return CGSize() 
    } 

    // subtract section left/ right insets mentioned in xib view 

    let widthAvailbleForAllItems = (collectionView.frame.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right) 

    // Suppose we have to create nColunmns 
    // widthForOneItem achieved by sunbtracting item spacing if any 

    let widthForOneItem = widthAvailbleForAllItems/nColumns - flowLayout.minimumInteritemSpacing 


    // here height is mentioned in xib file or storyboard 
    return CGSize(width: CGFloat(widthForOneItem), height: (flowLayout.itemSize.height)) 
} 
Powiązane problemy