Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
844 views
in Technique[技术] by (71.8m points)

iphone - Page count of UICollectionView with paging in iOS

Consider a UICollectionView with flow layout and paging enabled (by setting pagingEnabled to YES).

What would be the simplest way of obtaining the total number of pages?

And where would be the most appropriate place to update the total number of pages (given that it might change if items are added/deleted, the size of the collection view changes or the layout changes)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The right answer should be:

If the UICollectionView scrolls horizontally:

int pages = ceil(self.collectionView.contentSize.width /    
                   self.collectionView.frame.size.width);

If it scrolls vertically:

 int pages = ceil(self.collectionView.contentSize.height /    
                   self.collectionView.frame.size.height);

follow to wiki:

In mathematics and computer science, the floor and ceiling functions map a real number to the largest previous or the smallest following integer, respectively. More precisely, floor(x) is the largest integer not greater than x and ceiling(x) is the smallest integer not less than x.

Here is my result to check:

ceil(2.0/5.0) = 1.000000
ceil(5.0/5.0) = 1.000000
ceil(6.0/5.0) = 2.000000
ceil(10.0/5.0) = 2.000000
ceil(11.0/5.0) = 3.000000

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...