2013-09-25 4 views
15

Я хочу показать UICollectionView, который содержит ровно 2 строки и 100 ячеек в каждой строке.Укажите номер строки и столбца для 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; 
} 

Я получаю это:

enter image description here

Как я могу указать номер строки для UICollectionView? Я хочу создать таблицу 2x100.

ответ

11

Попробуйте это:

// 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

Большое спасибо ... – onivi

+42

кажется неправильным – aelam

+0

Лучше использовать пользовательский макет: http://stackoverflow.com/a/35362818/833885 – brianLikeApple

2

Количество секций диктует, сколько клеток будет в каждой строке, если предположить, что размер ячейки позволяет это.

Раздел всегда будет начинать новую строку.

13

Для 3 ячеек в строке .. Добавьте этот код.

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

Для этого метода необходим протокол UICollectionViewDelegateFlowLayout. –

+1

Да, нам нужно. Взгляните на: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewDelegateFlowLayout_protocol/ –

+0

Это должно быть 3 ячейки в строке или сколько столбцов. Правильно? – malajisi

9

Для Swift: 3 строк на колонку (При использовании @SakhshiSingla ответа)

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

Более общий подход к достижению п столбцов в представлении сбора, который совместит в любой ориентации

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)) 
} 
Смежные вопросы