iOS UICollectionViewController不显示中心单元格。但是可以点击它们

时间:2013-02-07 11:47:37

标签: uicollectionview uicollectionviewcell uicollectionviewlayout

我创建了一个自定义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

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

StoryBoard cell configuration Result in Simulator The images I expect from the photogallery

0 个答案:

没有答案