UICollectionView垂直显示最后一半的单元格

时间:2016-04-22 09:53:48

标签: ios objective-c iphone uicollectionview uicollectionviewlayout

我有collectionView。它垂直显示细胞。

在开始时,collectionView看起来像这样

start of the collectionview enter image description here

最后它看起来像这个

end of collectionView enter image description here

所以它基本上不会最终出现整个细胞 集合类的代码是

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

 }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - CollectionView Datasource

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

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

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ViewControllerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];



    return cell;
}
@end

3 个答案:

答案 0 :(得分:1)

您必须在故事板中指定单元格大小。

答案 1 :(得分:1)

- (CGSize)collectionView:(UICollectionView *)collectionView布局:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {

float width = 100;  //change to your needed width
float height = 100;  //change to your needed height

return CGSizeMake(width, height);

}

答案 2 :(得分:0)

试试这段代码它会帮助你

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


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cellIdentifier";
    UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];

    return cell;
}

并在属性检查器中更改标识符名称,如图所示:

enter image description here