Ios 指定UICollectionView的行和列编号
我想显示一个UICollectionView,它正好包含2行,每行包含100个单元格Ios 指定UICollectionView的行和列编号,ios,uicollectionview,uicollectionviewcell,Ios,Uicollectionview,Uicollectionviewcell,我想显示一个UICollectionView,它正好包含2行,每行包含100个单元格 // 1 - (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section { return 100; } // 2 - (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView
// 1
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return 100;
}
// 2
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return 2;
}
// 3
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.lbl.text = @"Data";
return cell;
}
我明白了:
如何为UICollectionView指定行号?我想创建一个2x100表。试试以下方法:
// 1
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return 2;
}
// 2
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return 100;
}
// 3
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.lbl.text = @"Data";
return cell;
}
假设单元格大小允许,则节数决定每行中有多少单元格
节将始终以新行开始 每行3个单元格。。添加此代码
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(collectionView.frame.size.width/3.2 , 100);
}
对于Swift:每列3行(使用@SakhshiSingla Answer)
在集合视图中实现n列的更通用方法是
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
// flow layout have all the important info like spacing, inset of collection view cell, fetch it to find out the attributes specified in xib file
guard let flowLayout = collectionViewLayout as? UICollectionViewFlowLayout else {
return CGSize()
}
// subtract section left/ right insets mentioned in xib view
let widthAvailbleForAllItems = (collectionView.frame.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right)
// Suppose we have to create nColunmns
// widthForOneItem achieved by sunbtracting item spacing if any
let widthForOneItem = widthAvailbleForAllItems / nColumns - flowLayout.minimumInteritemSpacing
// here height is mentioned in xib file or storyboard
return CGSize(width: CGFloat(widthForOneItem), height: (flowLayout.itemSize.height))
}
此方法需要
UICollectionViewDelegateFlowLayout
协议。是的,我们确实需要。看一看:@SakshiSingla为什么要用3.2?只是计算而已!您可以指定u的计算方式吗?我希望对每行的4个和5个单元格使用相同的计算方式。谢谢你确定没有其他关键步骤可以让这个方法发挥作用吗?函数在这里不调用。
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
// flow layout have all the important info like spacing, inset of collection view cell, fetch it to find out the attributes specified in xib file
guard let flowLayout = collectionViewLayout as? UICollectionViewFlowLayout else {
return CGSize()
}
// subtract section left/ right insets mentioned in xib view
let widthAvailbleForAllItems = (collectionView.frame.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right)
// Suppose we have to create nColunmns
// widthForOneItem achieved by sunbtracting item spacing if any
let widthForOneItem = widthAvailbleForAllItems / nColumns - flowLayout.minimumInteritemSpacing
// here height is mentioned in xib file or storyboard
return CGSize(width: CGFloat(widthForOneItem), height: (flowLayout.itemSize.height))
}