-->

iOS UICollectionViewController Not showing center

2019-07-29 23:00发布

问题:

I've created a custom UICollectionViewController.

I have 2 albums I want to display - each have a bit different functionality but the views are essentially the same (custom cell sizes , etc.). From this base ViewController I am inheriting the 2 customized UICollectionViewController with the specific behavior I am seeking.

The inheritance is

  • UICollectionViewController -> GalleyViewController (my base class) -> CameraAlbumViewController.

  • UICollectionViewController -> GalleyViewController (my base class) -> DifferentAlbumViewController

For some reason I am not seeing the center cells on the screen, but am able to click on them. I've tried changing the size of the cells, and then I see the 3 cells, but the boundaries of the cells are off (I can select between the cells and am getting the delegate methods called)

Thinking maybe that the inheritance was incorrect (perhaps missing something in the base class) I created a new project with the most basic use of the UICollectionViewController and followed several tutorials.

Still I am not seeing the center cells

From this I believe perhaps something in the storyboard is incorrect.

My code is this:

#import "MyCollectionViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>

@interface CollectionViewCell : UICollectionViewCell
// customized cell
@property (nonatomic,strong) UIImageView *imageView;
@end

@implementation CollectionViewCell

@synthesize imageView;
-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.imageView = [[UIImageView alloc] initWithFrame:frame];
        [self.contentView addSubview:self.imageView];
    }
    return self;
}

@end

@interface MyCollectionViewController ()
// the collection view controller
@property (nonatomic,strong) NSMutableArray *photos;
@property (nonatomic,strong) ALAssetsLibrary *defaultLibrary;
@end

@implementation MyCollectionViewController

@synthesize photos,defaultLibrary;

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

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    [self.collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [self initAssets];
}

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

-(void)initAssets
{
    self.photos = [NSMutableArray new];
    [self.photos removeAllObjects];
    self.defaultLibrary = [[ALAssetsLibrary alloc] init];
    [self.defaultLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
                                       usingBlock:^(ALAssetsGroup *group, BOOL *stop)
     {

         [group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
          {
              if (asset) {
                  [self.photos addObject:asset];

              }
          }];
         if (group == nil)
         {
             [self.collectionView reloadData];
         }
     } failureBlock:^(NSError *error)
     {
         NSLog(@"Error");
     }];


}

#pragma mark -
#pragma mark CollectionViewDelegates
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.photos.count;
}

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

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    ALAsset *asset = [self.photos objectAtIndex:indexPath.row];
    UIImage *im = [UIImage imageWithCGImage:[asset thumbnail]];
    cell.imageView.image = im;
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView
                 layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(100, 100);
}

@end

Some screenshots of the story board, camera album from the simulator