-->

的iOS UICollectionViewController未显示中心的细胞。 但是,可以点击

2019-10-17 16:26发布

我创建了一个自定义的UICollectionViewController

我有2个相册我想显示 - 每一个都具有不同的位的功能,但该视图是基本相同的(定制的小区大小,等等)。 从这个基地ViewController我继承了2定制UICollectionViewController与我要求的特定行为。

继承是

  • UICollectionViewController - > GalleyViewController(我的基类) - > CameraAlbumViewController。

  • UICollectionViewController - > GalleyViewController(我的基类) - > DifferentAlbumViewController

出于某种原因,我没有看到屏幕上的中心细胞,但我能点击它们。 我试着改变细胞的大小,然后我看到了3个细胞,但细胞的边界关闭(我能与细胞之间选择我得到所谓的委托方法)

想也许是继承是不正确的(也许是缺少在基类的东西),我创建了一个新的项目,最基本的使用的UICollectionViewController跟着几个教程。

不过我没有看到中心细胞

从这个我相信或许一些情节板中的不正确。

我的代码是这样的:

#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

故事板的一些截图,从模拟器相册

文章来源: iOS UICollectionViewController Not showing center cells. But can click on them