我有一个UICollectionView控件,我想在网格中显示图像。但是,当我运行代码时,它显示黑色的空白屏幕。
我有cell - ColViewCell
的单独课程,cell identifier=Cell
我也附上了课程。
我的代码如下。
ColViewController.h
#import <UIKit/UIKit.h>
@interface ColViewController : UICollectionViewController
@property (strong,nonatomic) NSArray *imgArray;
@end
ColViewController.m
#import "ColViewController.h"
#import "ColViewCell.h"
@interface ColViewController ()
@end
@implementation ColViewController
@synthesize imgArray;
static NSString * const reuseIdentifier = @"Cell";
- (void)viewDidLoad
{
imgArray=[NSArray arrayWithObjects:@"a1.png",@"a2.png",@"a3.png",@"a4.png",@"b1.png",@"b2.png",@"b3.png",nil];
self.collectionView.dataSource=self;
self.collectionView.delegate=self;
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [imgArray count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ColViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier
forIndexPath:indexPath];
UIImage *image1;
NSInteger row = [indexPath row];
image1 = [UIImage imageNamed:imgArray[row]];
cell.imageView.image=image1;
return cell;
}
@end
ColViewCell.h
#import <UIKit/UIKit.h>
@interface ColViewCell : UICollectionViewCell
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@end
ColViewCell.m
#import "ColViewCell.h"
@implementation ColViewCell
@synthesize imageView;
@end
答案 0 :(得分:1)
将此添加到ColViewController.m viewDidLoad方法
[collectionView registerClass:[ColViewCell class] forCellWithReuseIdentifier:@"Cell"];
答案 1 :(得分:0)
您没有在ViewDidload中注册 粘贴此视图已加载
[_collectionView registerNib:[UINib nibWithNibName:@"ColViewCell" bundle:nil] forCellWithReuseIdentifier: reuseIdentifier];