Leo Guo Leo Guo - 3 months ago 11
Swift Question

Swift: How to limit the numberOfItemInSection in CollectionView?

I have a UICollectionView used to display images downloaded from the Internet. I have an array used to store the data:

var post1: [Posts] = [Posts]()
. And in the

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return post1.count
}


the
post1.count
is 4 for example, but I just want to show the first 3 of them, how should I do to limit the
numberOfItemInSection
? thanks.

Answer
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return post1.count > 3 ? 3 : post1.count
}

By the way, if you want to show the first 3 of post1,you should use another array(which has 3 Posts) as collectionView's dataSource.

Comments