集合视图单元格大小不符合预期

时间:2017-08-20 12:44:15

标签: ios uicollectionview custom-keyboard

我正在尝试使用故事板制作自定义键盘。我需要使用两个集合视图。我希望集合视图单元格的高度和宽度为集合视图本身的高度。约束设置正确。但细胞大小并不像预期的那样。它与故事板上的高度集合视图相同。

Storyboard

Simulator

这是代码。

        #import "MainViewController.h"
        #import "UpperCollectionViewCell.h"
        #import "LowerCollectionViewCell.h"

        @interface MainViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

        @property (weak, nonatomic) IBOutlet UICollectionView *upperCollectionView;
        @property (weak, nonatomic) IBOutlet UICollectionView *lowerCollectionView;

        @property (strong, nonatomic) NSArray *upperData;
        @property (strong, nonatomic) NSArray *lowerData;

        @end

        @implementation MainViewController

        - (void)viewDidLoad {
        [super viewDidLoad];

        NSLog(@"View Presented");

        self.lowerCollectionView.delegate = self;
        self.lowerCollectionView.dataSource = self;
        self.upperCollectionView.delegate = self;
        self.upperCollectionView.dataSource = self;

        self.upperData = @[[[DataModel alloc] initWithTitle:@"Bollywood" subtitle:nil color:[UIColor orangeColor]],
                       [[DataModel alloc] initWithTitle:@"Hollywood" subtitle:nil color:[UIColor greenColor]],
                       [[DataModel alloc] initWithTitle:@"Tollywood" subtitle:nil color:[UIColor magentaColor]]];

        self.lowerData = @[[[DataModel alloc] initWithTitle:@"News" subtitle:@"Very first comment" color:[UIColor blueColor]],
                       [[DataModel alloc] initWithTitle:@"Tasks" subtitle:@"Second comment" color:[UIColor redColor]],
                       [[DataModel alloc] initWithTitle:@"Events" subtitle:@"Third comment" color:[UIColor greenColor]]];

        }

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

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

        - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

        if(collectionView == self.lowerCollectionView) {

        return self.lowerData.count;
        }

        else if(collectionView == self.upperCollectionView) {

        return self.upperData.count;
        }
        else {
        return 0;
        }
        }

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

        if(collectionView == self.lowerCollectionView) {

        LowerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"lower_cv_cell" forIndexPath:indexPath];
        [cell setDataModel:self.lowerData[indexPath.row]];
        [cell layoutIfNeeded];
        return cell;
        }

        else if(collectionView == self.upperCollectionView) {

        UpperCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"upper_cv_cell" forIndexPath:indexPath];
        [cell setDataModel:self.upperData[indexPath.row]];
        [cell layoutIfNeeded];
        return cell;

        }
        else {
        return nil;
        }
        }

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

        if(collectionView == self.lowerCollectionView) {

        CGSize size = CGSizeMake(collectionView.bounds.size.height, collectionView.bounds.size.height-10);

        NSLog(@"lower height : %@",NSStringFromCGSize(size));

        return size;
        }

        else if(collectionView == self.upperCollectionView) {

        CGSize size = CGSizeMake(collectionView.bounds.size.height, collectionView.bounds.size.height-10);
        NSLog(@"upper height : %@",NSStringFromCGSize(size));
        return size;
        }
        else {
        return CGSizeZero;
        }
        }

        @end

无法弄清楚为什么单元格大小不符合预期。

0 个答案:

没有答案