gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 03:31:41 31 4
gpt4 key购买 nike

我正在处理 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/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com