21

I want to show a UICollectionView which contains exactly 2 rows and 100 cells in each row.

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

I am getting this:

enter image description here

How can I specify a row number for UICollectionView? I want to create a 2x100 table.

Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
onivi
  • 1,326
  • 3
  • 17
  • 25

5 Answers5

19

Try this:

// 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;
}
amone
  • 3,133
  • 7
  • 29
  • 45
17

For 3 cells per row .. Add this code.

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
     return CGSizeMake(collectionView.frame.size.width/3.2 , 100);
}
Sakshi Singla
  • 511
  • 6
  • 5
14

The more general approach to achieve n columns in collection view which is compatible in any orientation is

 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))
 }
Vivek Bansal
  • 948
  • 9
  • 18
  • Are you sure there aren't any other critical steps to get this method to work? The function doesn't call over here. – JCutting8 Feb 01 '20 at 13:08
12

For Swift : 3 Rows per Column (By using @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)
    }
Naveed Ahmad
  • 6,199
  • 2
  • 54
  • 80
3

The Number of sections dictates how many cells will be in each row, assuming the cell size allows for it.

A section will always start a new row.

themikola
  • 124
  • 3