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

时间:2016-07-31 19:03:07

标签: ios swift uicollectionview

我目前有一个集合视图,它连接到服务器以获取数据并填充它。我怎样才能使得一旦3行(或x行数)变满而不是向集合视图中添加另一行,集合视图将变为水平滚动(实际上将新单元格添加到新列中,您必须向侧面滚动才能看到)?

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

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

或者在if方法中返回计数之前在numberOfItemsInSection语句中设置代码,并参考x与您返回的内容相关的内容,例如:

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
}