我有什么做的就是编程方式在屏幕上有两列的UICollectionView?(What I have

2019-09-03 21:14发布

我在新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与屏幕上的两列?

OSX 10.8.3的Xcode 4.6.2的iOS 6.1

Answer 1:

你需要切换你在重新调整和numberOfItems什么numberOfSections,让你只有1节,不以每节一个项目多个部分。 另外,请更改边的插图,以类似(5,60,5,60)。



文章来源: What I have to do to get the UICollectionView with two columns on the screen programmatically?
标签: iphone ios xcode