0

i foundthe UICollectionViewFlowLayout influnences where the new item was created but i dont't know why

It's my first time to use UICollectionView,and something i can't firgue out happened. Here are my code,and it's expected to show a row of 10 buttons.

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 10;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellidentifer forIndexPath:indexPath];
 
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    btn.frame = CGRectMake(50, 50, 40,40);
    btn.backgroundColor = [UIColor greenColor];
    
    [cell addSubview:btn];
    
    return cell;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

static NSString *cellidentifer = @"cell";

- (void)viewDidLoad
{
    [super viewDidLoad];

    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
    
    _col = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowlayout];
    _col.backgroundColor = [UIColor whiteColor];
    _col.contentSize = CGSizeMake(500, 500);
    _col.dataSource = self;
    _col.delegate = self;
    
    [_col registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:cellidentifer];
    
    [self.view addSubview:_col];
}

and this is what i get

result image

then i just add a line in my code to change the * direction like this

    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc]init];
    flowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

it gave me this

enter image description here

finally i change the ,something even stranger happened,BTW,it can't be scrolled,neither vertical nor horizontal

enter image description here

i am totally confused now ,thank you

Community
  • 1
  • 1
Wythe
  • 149
  • 3
  • 12

1 Answers1

0

What you're seeing when you change the scroll direction to horizontal is normal. With a flow layout, the cells are filled up first along a row (if the scrolling is vertical) until you reach the end of the content view, then it goes to the next row and fills that until all the cell are laid out. If the scrolling is horizontal, it fills the first column going down, then goes to the next column, so that's what you see in your second image. I have no idea what's happening in your third image, you need to provide more information; what size did you set the button frame to that gave that result?

rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • this is my Collection contentsize [tag:_col.contentSize=CGSizeMake(500, 500);] this is button size i give in collection delegate method btn.frame = CGRectMake(50, 50, 40,40); – Wythe Sep 20 '14 at 06:40
  • @myqiqiang, what size are the cells, and have you set the inter item and inter line spacings to something? – rdelmar Sep 20 '14 at 15:28
  • i didn't set the cell size, what i have given in question is my whole code except .h file.and id .h file i just declare some vars – Wythe Sep 22 '14 at 03:12