iOS UICollectionViewController Not showing center cells. But can click on them

折月煮酒 提交于 2019-12-11 04:45:23

问题


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

来源:https://stackoverflow.com/questions/14750402/ios-uicollectionviewcontroller-not-showing-center-cells-but-can-click-on-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!