简体   繁体   English

集合视图在设置行数后水平滚动? iOS Swift

[英]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)? 我该如何确保一旦3行(或x行的数量)已满,而不是将另一行添加到集合视图中,则集合视图将变为水平滚动(实质上是将新单元格添加到必须横向滚动才能看到的新列中) )?

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: 或者在返回numberOfItemsInSection方法中的计数并在返回的结果中引用x之前,在if语句内设置代码,例如:

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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