简体   繁体   中英

collection view horizontal scroll after set number of rows ? ios Swift

I currently have a collection view that connects to a server to get data and fill it up. How can I make it that once 3 rows (or x amount of rows) get full instead of adding another row to the collection view the collection view would become horizontally scrolling (essentially adding the new cells into new columns that you must scroll sideways to see)?

You could try something like this:

if yourCollectionView.numberOfItemsInSection(0) >= x {
   let layout = yourCollectionView.collectionViewLayout as! UICollectionViewFlowLayout    
   layout.scrollDirection = .Horizontal
   yourCollectionView.collectionViewLayout = layout
}

Or set the code inside of the if statement before you return the count in the numberOfItemsInSection method and refer to x in relation to what you return, for example:

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    let countToReturn = yourDataSource.count

    if countToReturn >= x {
        let layout = yourCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.scrollDirection = .Horizontal
        yourCollectionView.collectionViewLayout = layout
    }

    return countToReturn
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM