我需要做些什么才能以编程方式在屏幕上获得两列的UICollectionView? [英] What I have to do to get the UICollectionView with two columns on the screen programmatically?
问题描述
我是 UICollectionView
的新手,所以我决定以编程方式使用 UICollectionView
创建一个测试.因此,我在Xcode中创建了一个新的 SingleViewApplication
项目,并编写了以下代码:
I'm new in UICollectionView
and I decided to create a test using UICollectionView
programmatically. So I created a new SingleViewApplication
project in Xcode and wrote the follow code:
@interface GzViewController ()
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *items;
@end
@implementation GzViewController
- (void)viewDidLoad{
[super viewDidLoad];
self.items = @[@"Address Book", @"Contact", @"Email", @"Phone", @"SMS", @"Free Text", @"URL", @"WiFi"];
UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
self.collectionView = [[UICollectionView alloc] initWithFrame:[self.view frame] collectionViewLayout:flowLayout];
[self.collectionView setDataSource:self];
[self.collectionView setDelegate:self];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
[self.view addSubview:self.collectionView];
}
#pragma mark - UICollectionView Datasource
- (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section {
return 1;
}
- (NSInteger)numberOfSectionsInCollectionView: (UICollectionView *)collectionView {
return [self.items count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [cv dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
#pragma mark – UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize retval = CGSizeMake(80, 80);
return retval;
}
- (UIEdgeInsets)collectionView:
(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
return UIEdgeInsetsMake(5, 20, 5, 5);
}
结果是这样的:
我要怎么做才能在屏幕上显示两列的 UICollectionView
?
What I have to do to get the UICollectionView
with two columns on the screen ?
OSX 10.8.3Xcode 4.6.2iOS 6.1
OSX 10.8.3 Xcode 4.6.2 iOS 6.1
推荐答案
您需要在numberOfItems和numberOfSections中切换要调整的内容,以使您只有一个部分,而没有多个部分,每个部分只有一个.另外,将边缘插图更改为(5,60,5,60).
You need to switch what you're retuning in numberOfItems and numberOfSections, so that you have only 1 section, not multiple sections with one item per section. Also, change your edge insets to something like (5,60,5,60).
这篇关于我需要做些什么才能以编程方式在屏幕上获得两列的UICollectionView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!