3

我是新手,UICollectionView我决定以UICollectionView编程方式创建一个测试。所以我SingleViewApplication在 Xcode 中创建了一个新项目并编写了以下代码:

@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

我必须做什么才能UICollectionView在屏幕上显示两列?

OSX 10.8.3 Xcode 4.6.2 iOS 6.1

4

1 回答 1

6

您需要在 numberOfItems 和 numberOfSections 中切换要重新调整的内容,以便您只有 1 个部分,而不是多个部分,每个部分一个项目。此外,将边缘插图更改为 (5,60,5,60)。

于 2013-05-17T00:46:07.010 回答