仅显示两列,使用故事板在CollectionView中显示多行 [英] Display just two columns, with multiple rows in a CollectionView using storyboard
问题描述
无论iPhone的屏幕尺寸是多少,我都想连续显示两个单元格。比如,
I want to display only two cells in a row, no matter what the iPhone screen size is. Like,
我的故事板包含 UICollectionView
,通过约束连接。
My storyboard contains a UICollectionView
,connected by constraints.
<$的故事板设置c $ c> UICollectionView 是,
现在,当我运行这个时在6,5s或更低的情况下,连续出现一个单元格。我使用的代码是,
Now, when I run this in 6, 5s or below, only one cell appears in a row. The code I'd used is,
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [categoryImages count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
homePageCells *cell = (homePageCells *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cells" forIndexPath:indexPath];
cell.categoryName.text = [categoryNames objectAtIndex:indexPath.row];
return cell;
}
我尝试检测屏幕大小并使用代码以编程方式分配适当的单元格大小,
I tried detecting the screen size and assigning appropriate cell size programmatically using the code,
- (CGSize)collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
CGSize sizes;
CGSize result = [[UIScreen mainScreen] bounds].size;
NSLog(@"%f",result.height);
if(result.height == 480)
{
//Load 3.5 inch xib
sizes =CGSizeMake(170.f, 170.f);
NSLog(@"3gs");
}
else if(result.height == 568)
{
//Load 4 inch xib
sizes =CGSizeMake(130.f, 130.f);
NSLog(@"5s");
}
else if(result.height == 667.000000)
{
//Load 4.7 inch xib
sizes =CGSizeMake(160.f, 160.f);
NSLog(@"6");
}
else
{
NSLog(@"else");
sizes =CGSizeMake(170.f, 165.f);
}
return sizes;
}
但我也知道这不是正确的方法,所以请给我是一个正确的方法来处理这个问题。
But I also know that this isn't the right way, so please give me a right way to handle this.
推荐答案
你可以这样做,你不需要检查设备大小我们可以使用 tableView
宽度来计算单元格的宽度&据此,我们可以提升我们的身高。
You can do like this, You need not to check for device sizes as we can use the tableView
width to calculate the width of the cell & according to which we can upadate our height.
你需要再使用一件事 CollectionViewFlowLayout
&确认代表和实现方法
One More thing you need to use CollectionViewFlowLayout
& confirm the delegate & implement method below
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
let padding: CGFloat = 50
let collectionViewSize = collectionView.frame.size.width - padding
return CGSizeMake(collectionViewSize/2, collectionViewSize/2)
}
更新:Swift 4.0
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let padding: CGFloat = 50
let collectionViewSize = collectionView.frame.size.width - padding
return CGSize(width: collectionViewSize/2, height: collectionViewSize/2)
}
注意:我已经回答了这个问题单元格是正方形的
Note: I have answered the question considering the cells are square sized
这篇关于仅显示两列,使用故事板在CollectionView中显示多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!