ios 为 UICollectionView 指定行号和列号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19010335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Specify row and column number for UICollectionView
提问by onivi
I want to show a UICollectionView which contains exactly 2 rows and 100 cells in each row.
我想显示一个 UICollectionView,它正好包含 2 行和每行 100 个单元格。
// 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;
}
I am getting this:
我得到这个:
How can I specify a row number for UICollectionView? I want to create a 2x100 table.
如何为 UICollectionView 指定行号?我想创建一个 2x100 的表。
采纳答案by amone
Try this:
尝试这个:
// 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;
}
回答by Sakshi Singla
For 3 cells per row .. Add this code.
每行 3 个单元格 .. 添加此代码。
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(collectionView.frame.size.width/3.2 , 100);
}
回答by Vivek Bansal
The more general approach to achieve n columns in collection view which is compatible in any orientation is
在任何方向兼容的集合视图中实现 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))
}
回答by Naveed Ahmad
For Swift: 3 Rows per Column(By using @SakhshiSingla Answer)
对于 Swift:每列 3 行(通过使用 @SakhshiSingla Answer)
override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
return CGSize(width: collectionView.frame.size.width/3.2, height: 100)
}
回答by themikola
The Number of sections dictates how many cells will be in each row, assuming the cell size allows for it.
部分的数量决定了每行中有多少个单元格,假设单元格大小允许。
A section will always start a new row.
一个部分将始终开始一个新行。