ios - 如何在ios中获取一个部分的行数

标签 ios objective-c uicollectionview

我正在处理 Collection View ,我想要上一节中的行数。 是否有任何方法返回部分中的行数。 请帮助我。

最佳答案

UICollectionView dataSource 的委托(delegate)方法可以手动调用您需要实现以显示单元格的方法。

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

因此你可以这样调用它:

NSInteger numberOfRows = [self collectionView:self.collectionView numberOfItemsInSection:MAX(0, currentSection - 1)];

你不想得到越界异常所以我把它限制在 0*

编辑

@holex 提出了一个很好的观点——我假设你只有一列。

如果您知道项目宽度的大小,您可以执行以下操作:

//Assuming a fixed width cell

NSInteger section = 0;
NSInteger totalItemsInSection = [self.feedCollectionView numberOfItemsInSection:section];

UIEdgeInsets collectionViewInset = self.feedCollectionView.collectionViewLayout.sectionInset;
CGFloat collectionViewWidth = CGRectGetWidth(self.feedCollectionView.frame) - collectionViewInset.left - collectionViewInset.right;
CGFloat cellWidth = [self.feedCollectionView.collectionViewLayout itemSize].width;
CGFloat cellSpacing = [self.feedCollectionView.collectionViewLayout minimumInteritemSpacing];

NSInteger totalItemsInRow = floor((CGFloat)collectionViewWidth / (cellWidth + cellSpacing));
NSInteger numberOfRows = ceil((CGFloat)totalItemsInSection / totalItemsInRow);

NSLog(@"%@", @(numberOfRows));

我们确定一行中将显示多少项,以便能够确定该部分中的行数。

关于ios - 如何在ios中获取一个部分的行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25094696/

相关文章:

ios - UICollectionViewCell 填充了错误的数据

ios - 使用 Windows 为 IOS 构建 Expo 应用程序

ios - 以编程方式创建 UICollectionView(在 UIViewController 之外)

iOS UIImage 不显示 JPEG 图像

objective-c - 对象为零,除非我添加另一行 'random' 代码

ios - GPPSignIn 因 NSInvalidArgumentException 而崩溃

iphone - 代码中使用的 iOS 谷歌地图方向 API

ios - 我们可以在 UICollectionView 的不同部分设置不同的流布局吗?

ios - OpenGLES阴影体积

ios - 可以使用 Adob​​e Air SDK 进行应用内购买