Monsieur Candie
Monsieur Candie

Reputation: 1

Can we specify the number of rows that can be shown in each section of a collection view

Im currently working on a TvOS App, in this i have one collection view with multiple sections and it has multiple rows under it. Will it be possible to limit the number of rows per section as 1?

I have tried using minimumLineSpacingForSectionAt function delegate

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
   return 1
}

This is not working and if i try to move my focus the row in the section below also starts to Move, Please help me find a solution for this problem.

Upvotes: 0

Views: 867

Answers (3)

PGDev
PGDev

Reputation: 24341

You need to,

  1. return 1 in UICollectionViewDataSource's collectionView(_:numberOfItemsInSection:) method and
  2. return sectionsCount in numberOfSections(in:), where sectionsCount is the total number of sections.

This is what you should implement to get that working,

func numberOfSections(in collectionView: UICollectionView) -> Int {
    return sectionsCount
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 1
}

The method that you've implemented has a different purpose and doesn't define the items/sections in the collectionView.

collectionView(_:,layout:,minimumLineSpacingForSectionAt:)

Asks the delegate for the spacing between successive rows or columns of a section.

Upvotes: 0

Pratik
Pratik

Reputation: 726

You need to use a custom layout for that. Please refer to this Link for an example like Pinterest in Swift 4

Similar question: Custom layout for collection view in swift?

Upvotes: 0

vadian
vadian

Reputation: 285190

minimumLineSpacingForSectionAt has nothing to do with the number of sections or rows.

You have to maintain your data source array that each section array contains only one item.

Or if you actually have an one-dimensional data source array return 1 in numberOfRows and datasourcearray.count in numberOfSections. Then you have datasourcearray.count sections with one row respectively.

Upvotes: 2

Related Questions