我创建了一个自定义UICollectionViewController
.
我有 2 个要显示的专辑 - 每个专辑都有一些不同的功能,但视图基本相同(自定义单元格大小等)。从这个基础ViewController
上,我继承了UICollectionViewController
我正在寻找的特定行为定制的 2。
遗产是
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
一些故事板的截图,来自模拟器的相册